txt = "The rain in Spain stays mainly in the plain"x = "ain" in txtprint(x) // Truetxt = "The rain in Spain stays mainly in the plain "x = " " in txtprint(x) // Truetxt = "The rain in Spain stays mainly in the plain "x = " " in txt.strip()print(x) // True ,即使在刪除空格之后,它也是真實的。
4 回答

一只甜甜圈
TA貢獻1836條經驗 獲得超5個贊
txt = "The rain in Spain stays mainly in the plain "
txt.strip()
輸出:
'The rain in Spain stays mainly in the plain'
stript()只是刪除開頭和結尾的空格,而不是字符串之間的空格。因此,以下陳述是正確的:
x = " " in txt.strip()
print(x)
輸出:
True
有很多方法可以刪除字符串中的所有空格:
1.split()
''.join(txt.split())
2.replace()
txt.replace(" ", "")
3、正則表達式:
import re
stringWithoutSpaces = re.sub(r"\s+", "", txt, flags=re.UNICODE)
所有這些的輸出是:
'TheraininSpainstaysmainlyintheplain'
添加回答
舉報
0/150
提交
取消