Python 字符串“in”运算符

txt = "The rain in Spain stays mainly in the plain"

x = "ain" in txt

print(x) // True


txt = "The rain in Spain stays mainly in the plain "

x = " " in txt

print(x) // True


txt = "The rain in Spain stays mainly in the plain "

x = " " in txt.strip()


print(x) // True ,

即使在删除空格之后,它也是真实的。


白衣染霜花
浏览 133回答 4
4回答

子衿沉夜

strip()仅删除前导和尾随空格,而不是字符串中的所有空格。

繁花不似锦

txt.replace(" ", "")这将删除所有空格。如果您还想删除其他空格,txt = "".join(txt.split())

一只甜甜圈

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 restringWithoutSpaces = re.sub(r"\s+", "", txt, flags=re.UNICODE)所有这些的输出是:'TheraininSpainstaysmainlyintheplain'

慕勒3428872

strip() 删除前导字符和尾随字符。意味着文本看起来像这样“西班牙的雨主要停留在平原”但单词之间仍然有空格
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python