gpt4 book ai didi

python - 字符串的endswith()函数给出了矛盾的结果

转载 作者:行者123 更新时间:2023-12-04 13:42:09 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Unexpected empty strings within Python strings

(1 个回答)


2年前关闭。




当我注意到四个简单的一行语句的这四个结果时,我只是在 Python 控制台中闲逛:
1. "x".endswith("")
True
2. "x".endswith("x")
True

然后我试图从语句中去除空格,当他们给我这些结果时:
3. "x".strip().endswith("")
True
4. "x".strip().endswith("x")
True

怎么可能所有的结果都是True ?字符串函数如何返回 True以两者结尾 "x""" ?这不是矛盾的还是我在这里遗漏了什么?

Windows 10 上 PyCharm 上的 Python 2.7。

最佳答案

如果您想检查字符串是否以空格结尾,请尝试 "x "
例如,"x ".endswith(" ")返回 True ,但是 "x ".strip().endswith(" ")返回 False .
""是一个空字符,你试图做的事情是行不通的。空格字符显示如下:" " .

关于python - 字符串的endswith()函数给出了矛盾的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55650286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com