繁体   English   中英

如何使用python从元组中删除特定字符

[英]how to remove a specific character from a tuple using python

我有一个元组:

tuple = ('one', ('two', 'three'))

我想打印从这个元组中删除'以便我可以将它打印为(one, (two, three))

我试过这个

tuple = str(tuple)
print tuple.strip(')

但我收到此错误:

    print tuple.strip(')
                       ^
SyntaxError: EOL while scanning string literal

如何将我的元组转换为所需的字符串?

t = ('one', ('two', 'three'))
t2 = str(t)       
print(t2.replace("'","")) 

使用像tuple这样的关键字作为变量名被认为是不好的做法,你可能会遇到麻烦。

您可以使用替换打印:

print(str(tuple).replace("'", ''))

输出

(one, (two, three))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM