tags:

views:

74

answers:

3

I have a tuple of strings that i would want to extract the contents as a quoted string, i.e.

tup=('string1', 'string2', 'string3')

when i do this

main_str = ",".join(tup)

#i get

main_str = 'string1, string2, string3'

#I want the main_str to have something like this

main_str = '"string1", "string2", "string3"'

Gath

+7  A: 
", ".join('"{0}"'.format(i) for i in tup)

or

", ".join('"%s"' % i for i in tup)
SilentGhost
+1  A: 

Well, one answer would be:

', '.join([repr(x) for x in tup])

or

repr(tup)[1:-1]

But that's not really nice. ;)

Updated: Although, noted, you will not be able to control if resulting string starts with '" or '". If that matters, you need to be more explicit, like the other answers here are:

', '.join(['"%s"' % x for x in tup])
Lennart Regebro
missing closing angled bracket
gath
repr(x) should be used if the strings in tup might have quotes in them and you care (say, you want to retrieve what is in the quotes).
Kathy Van Stone
A: 

Here's one way to do it:

>>> t = ('s1', 's2', 's3')
>>> ", ".join( s.join(['"','"']) for s in t)
'"s1", "s2", "s3"'
Mark Rushakoff