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
提交
取消