I’m searching a simple method for converting a list of strings (e.g. ['test 1', 'test 2' , 'test 3']
) into a string whereby the list of objects doesn’t have some quotation mark. That means in our example ('[test 1, test 2, test 3]'
). My first approach was:
JavaScript
x
4
1
import json
2
A = ['test 1', 'test 2' , 'test 3']
3
test = json.dumps(A).replace('"', '')
4
Is their a more general way without that replace statement at the background? Cause my problem with that is, that e.g.
JavaScript
1
2
1
A = ['test 1', 'test 2' , 'test 3"AA"']
2
results in the string:
JavaScript
1
2
1
'[test 1, test 2, test 3AA]'
2
and not the desired string:
JavaScript
1
2
1
'[test 1, test 2, test 3"AA"]'
2
Advertisement
Answer
Try f-strings
:
JavaScript
1
8
1
>>> print(f"'[{', '.join(A)}]'")
2
'[test 1, test 2, test 3"AA"]'
3
4
# OR
5
6
>>> print(f"[{', '.join(A)}]")
7
[test 1, test 2, test 3"AA"]
8