Python 元组到字符串

Python 元组到字符串,python,Python,我有一个元组 tst = ([['name', u'bob-21'], ['name', u'john-28']], True) 我想把它转换成一个字符串 print tst2 "([['name', u'bob-21'], ['name', u'john-28']], True)" 做这件事的好方法是什么 谢谢 tst2 = str(tst) 例如: 虽然我喜欢Adam关于str()的建议,但我倾向于repr(),因为您正在明确寻找类似python语法的对象表示形式。判断help(str

我有一个元组

tst = ([['name', u'bob-21'], ['name', u'john-28']], True)
我想把它转换成一个字符串

print tst2
"([['name', u'bob-21'], ['name', u'john-28']], True)"
做这件事的好方法是什么

谢谢

tst2 = str(tst)
例如:


虽然我喜欢Adam关于
str()
的建议,但我倾向于
repr()
,因为您正在明确寻找类似python语法的对象表示形式。判断
help(str)
,它对元组的字符串转换在未来的版本中可能会有不同的定义

class str(basestring)
 |  str(object) -> string
 |
 |  Return a nice string representation of the object.
 |  If the argument is a string, the return value is the same object.
 ...
帮助(repr)
相反:

然而,在当今的实践和环境中,两者之间几乎没有什么区别,所以请使用最能描述您的需求的东西—您可以反馈给
eval()
,或者用于用户消费的东西

>>> str(tst)
"([['name', u'bob-21'], ['name', u'john-28']], True)"
>>> repr(tst)
"([['name', u'bob-21'], ['name', u'john-28']], True)"

谢谢你,亚当。我曾想过使用str,但从未想过它会起作用!
repr(...)
    repr(object) -> string

    Return the canonical string representation of the object.
    For most object types, eval(repr(object)) == object.
>>> str(tst)
"([['name', u'bob-21'], ['name', u'john-28']], True)"
>>> repr(tst)
"([['name', u'bob-21'], ['name', u'john-28']], True)"