[英]Python string strip function
為什么以下代碼可以刪除“ +”:
a = '+'
a.strip('+')
#output: ''
a = '1+'
a.strip('+')
#output: '1'
a = '+66'
a.strip('+')
#output: '66'
但是以下內容不能:
a = '1+2'
a.strip('+')
#output: '1+2'
為什么?
strip()
函數僅刪除字符串外部的前導和尾隨字符。 由於在您的最后一個示例中, +
位於中間,因此不會將其刪除。 也許嘗試使用replace()
代替:
my_str = "1+2"
new_str = my_str.replace("+", "")
strip
僅刪除字符串中指定的標題和結尾字符,而不刪除中間的字符串。
同樣, rstrip
僅刪除尾隨的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.