简体   繁体   English

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

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

I have a tuple:我有一个元组:

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

I want to print remove the ' from this tuple so I can print it as (one, (two, three)) .我想打印从这个元组中删除'以便我可以将它打印为(one, (two, three))

I have tried this我试过这个

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

But I get this error:但我收到此错误:

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

How can I turn my tuple into the desired string?如何将我的元组转换为所需的字符串?

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

It's considered bad practice to use keywords like tuple as variable names, you might run into trouble with that.使用像tuple这样的关键字作为变量名被认为是不好的做法,你可能会遇到麻烦。

You can print with replace:您可以使用替换打印:

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

output输出

(one, (two, three))

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

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