python中的startswith函数

a = "hello world and itcast and hello itheima and PYTHON"
print(a.startswith("hello",0,4))

为什么结果是false呢,0到四个字符明明是hello,满足hello开头啊

a = "hello world and itcast and hello itheima and PYTHON"
print(a.startswith("hello",0,5))

hello的长度是5

如果对你有帮助,可以点击我这个回答右上方的【采纳】按钮,给我个采纳吗,谢谢

hello 是5个字符。应该是

print(a.startswith("hello",0,5))

startswith 的语法说明:

.startswith(prefix, start, end)

如果有可选项 start,将从所指定位置开始检查。 如果有可选项 end,将在所指定位置停止比较。

注意是 指定位置停止比较 ,end=4,就是到下标是4就不比了。只比较0123下标的字符。


 

字符串的starswith方法的用法是:str.startswith(str, beg=0,end=len(string));其中end参数是指查找字符串的长度,不是下标。所以代码中第三个参数应该是5。

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632