我有一个字符串元组,我想将内容作为带引号的字符串提取,即

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"'

加特

有帮助吗?

解决方案

", ".join('"{0}"'.format(i) for i in tup)

", ".join('"%s"' % i for i in tup)

其他提示

嗯,答案是:

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

repr(tup)[1:-1]

但那并不是很好。 ;)

更新: 虽然,请注意,您将无法控制结果字符串是否以“<!>”开头;或'<!>;如果这很重要,你需要更加明确,就像其他答案一样:

', '.join(['"%s"' % x for x in tup])

这是一种方法:

>>> t = ('s1', 's2', 's3')
>>> ", ".join( s.join(['"','"']) for s in t)
'"s1", "s2", "s3"'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top