Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: converting list of strings into a string which illustrates the list

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:

import json
A = ['test 1', 'test 2' , 'test 3']
test = json.dumps(A).replace('"', '')

Is their a more general way without that replace statement at the background? Cause my problem with that is, that e.g.

A = ['test 1', 'test 2' , 'test 3"AA"']

results in the string:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   '[test 1, test 2, test 3\AA\]'

and not the desired string:

'[test 1, test 2, test 3"AA"]'

>Solution :

Try f-strings:

>>> print(f"'[{', '.join(A)}]'")
'[test 1, test 2, test 3"AA"]'

# OR

>>> print(f"[{', '.join(A)}]")
[test 1, test 2, test 3"AA"]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading