175

I've got:

words = ['hello', 'world', 'you', 'look', 'nice']

I want to have:

'"hello", "world", "you", "look", "nice"'

What's the easiest way to do this with Python?

philipxy
  • 14,867
  • 6
  • 39
  • 83
kadrian
  • 4,761
  • 8
  • 39
  • 61

9 Answers9

305

Update 2021: With f strings in Python3

>>> words = ['hello', 'world', 'you', 'look', 'nice']
>>> ', '.join(f'"{w}"' for w in words)
'"hello", "world", "you", "look", "nice"'

Original Answer (Supports Python 2.6+)

>>> words = ['hello', 'world', 'you', 'look', 'nice']
>>> ', '.join('"{0}"'.format(w) for w in words)
'"hello", "world", "you", "look", "nice"'
jamylak
  • 128,818
  • 30
  • 231
  • 230
  • 1
    @Meow that uses `repr` which is a lil hacky in this specific case as opposed to being clear with the quotes – jamylak May 03 '17 at 02:47
  • 1
    @jamlak ok, repr just seemed safer to me incase you have quotes in your string. – Meow May 03 '17 at 23:29
  • @Meow good point about the quotes, I guess that this one has the advantage of being explicit while the other approach does support the quotes thing – jamylak Aug 08 '22 at 04:37
76

You can try this :

str(words)[1:-1]
Thykof
  • 895
  • 7
  • 9
56

you may also perform a single format call

>>> words = ['hello', 'world', 'you', 'look', 'nice']
>>> '"{0}"'.format('", "'.join(words))
'"hello", "world", "you", "look", "nice"'

Update: Some benchmarking (performed on a 2009 mbp):

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.32559704780578613

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(words))""").timeit(1000)
0.018904924392700195

So it seems that format is actually quite expensive

Update 2: following @JCode's comment, adding a map to ensure that join will work, Python 2.7.12

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.08646488189697266

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(map(str, words)))""").timeit(1000)
0.04855608940124512

>>> timeit.Timer("""words = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.17348504066467285

>>> timeit.Timer("""words = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] * 100; '"{}"'.format('", "'.join(map(str, words)))""").timeit(1000)
0.06372308731079102
marchelbling
  • 1,909
  • 15
  • 23
  • does this have a better performance than the one proposed by jamylak? – kadrian Aug 17 '12 at 15:27
  • 3
    This is a better solution than the accepted answer. – sage88 Apr 17 '15 at 02:53
  • 2
    @sage88 No it isn't. Preoptimization is evil, there is a 0.0000001% chance that the miniscule speed difference here is the entire bottleneck of a Python script. Also this code is much less intuitive so it is not *better*, it is *very slightly faster*. My solution is more pythonic and readable – jamylak Oct 15 '16 at 01:14
  • @marchelbling Benchmark is invalid because jamylak' solution works also for iterables of non-strings. Replace `.join(words)` with `.join(map(str, words))` and show us how that goes. – WloHu Oct 19 '17 at 07:51
  • @JCode updated the benchmark. The gap is smaller but there still is a 2x gain on my machine. – marchelbling Oct 19 '17 at 13:33
  • @marchelbling Keep in mind that `map` in Python 2 returns a `list` but in Python 3 `map object` generator is returned. I suggested `map` to express the idea and I didn't know that you used Python 2. – WloHu Oct 20 '17 at 15:37
  • If there are no items in the list this returns `""` empty double quotes instead of an empty string. – bdrx Oct 14 '20 at 16:57
9
>>> ', '.join(['"%s"' % w for w in words])
jamylak
  • 128,818
  • 30
  • 231
  • 230
Emmett Butler
  • 5,969
  • 2
  • 29
  • 47
6

An updated version of @jamylak answer with F Strings (for python 3.6+), I've used backticks for a string used for a SQL script.

keys = ['foo', 'bar' , 'omg']
', '.join(f'`{k}`' for k in keys)
# result: '`foo`, `bar`, `omg`'
Gruber
  • 2,196
  • 5
  • 28
  • 50
1
words = ['hello', 'world', 'you', 'look', 'nice']
S = ""
for _ in range(len(words)-1):
    S+=f'"{words[_]}"'+', '
S +=f'"{words[len(words)-1]}"'
print("'"+S+"'")

OUTPUT:

'"hello", "world", "you", "look", "nice"'
Bhaskar Gupta
  • 109
  • 1
  • 7
0

find a faster way

'"' + '","'.join(words) + '"'

test in Python 2.7:

    words = ['hello', 'world', 'you', 'look', 'nice']

    print '"' + '","'.join(words) + '"'
    print str(words)[1:-1]
    print '"{0}"'.format('", "'.join(words))

    t = time() * 1000
    range10000 = range(100000)

    for i in range10000:
        '"' + '","'.join(words) + '"'

    print time() * 1000 - t
    t = time() * 1000

    for i in range10000:
        str(words)[1:-1]
    print time() * 1000 - t

    for i in range10000:
        '"{0}"'.format('", "'.join(words))

    print time() * 1000 - t

The resulting output is:

# "hello", "world", "you", "look", "nice"
# 'hello', 'world', 'you', 'look', 'nice'
# "hello", "world", "you", "look", "nice"
# 39.6000976562
# 166.892822266
# 220.110839844
geertjanvdk
  • 3,440
  • 24
  • 26
Luna
  • 1
  • 1
0

There are two possible solution and for me we should choose wisely...

items = ['A', 'B', 'C']

# Fast since we used only string concat.
print("\"" + ("\",\"".join(items)) + "\"")

# Slow since we used loop here.
print(",".join(["\"{item}\"".format(item=item) for item in items]))
-1
# Python3 without for loop
conc_str = "'{}'".format("','".join(['a', 'b', 'c']))
print(conc_str) 

# "'a', 'b', 'c'"
RiveN
  • 2,595
  • 11
  • 13
  • 26
abba
  • 1
  • 1
  • This is a blatant duplicate to this [answer](https://stackoverflow.com/a/12008055/15521392)....10 years later. – Rabinzel Aug 21 '22 at 07:08