-1

I have the following list of strings:

a = ['a','the quick fox', 'b', 'c', 'hi there']

How can I transform it into:

'a','the quick fox', 'b', 'c', 'hi there'

I tried to:

 "','".join(a)

However, its returning me this:

"hey','the quick fox','b','c','hi there"

Instead of:

'hey','the quick fox','b','c','hi there'
J.Doe
  • 39
  • 7

1 Answers1

0

Instead of adding the quotes as part of the join, you can instead get the repr representation of each string, which will include the quotes, then join those:

print(', '.join(map(repr, a)))
# 'a', 'the quick fox', 'b', 'c', 'hi there'
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • now I end up with something like this: `"'a', 'the quick fox', 'b', 'c', ""hi there'` – J.Doe Nov 29 '18 at 21:37
  • The `join` will produce the string `"'a', 'the quick fox', 'b', 'c', 'hi there'"`, which when printed looks like `'a', 'the quick fox', 'b', 'c', 'hi there'`, just like the string `"dog"` looks like `dog` when printed. Is that not what you want? – Patrick Haugh Nov 29 '18 at 21:48
  • Do not confuse the output you see in the Python console with the actual contents of a variable. This answer is giving you the correct value, but just doing this `', '.join(map(repr, a))` at the Python prompt will show you the repr of the value, which wraps strings in single or double quotes, to show that they are strings. Try assigning to a variable like this `list_as_str = ', '.join(map(repr, a))`, and then print out `list_as_str` using `print(list_as_str)`. If at the Python prompt you just do `list_as_str` to see its contents, you will get the repr again. – PaulMcG Nov 29 '18 at 21:54