我有一个元组:
tuple = ('one', ('two', 'three'))
我想打印删除 ' 从这个元组中,我可以把它打印成 (one, (two, three)) .
'
(one, (two, three))
我试过了
tuple = str(tuple) print tuple.strip(')
但我有个错误:
print tuple.strip(') ^ SyntaxError: EOL while scanning string literal
如何将元组转换为所需的字符串?
您可以打印替换:
print(str(tuple).replace("'", ''))
输出
t = ('one', ('two', 'three')) t2 = str(t) print(t2.replace("'",""))
使用关键字如 tuple 作为变量名,您可能会遇到麻烦。
tuple