簡體   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