Skip to main content
 首页 » 编程设计

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

2025年05月04日9bhlsheji

我有一个元组:

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 这样的关键字作为变量名被认为是不好的做法,你可能会遇到麻烦。