python startswith 函数使用实例
startswith() 函数用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。
以下是 startswith() 函数的语法:
str.startswith(sub, start=0, end=len(string))
参数说明:
- sub:要检索的子字符串。
- start:字符串中的开始位置。
- end:字符串中的结束位置。
示例:
str1 = "Hello, world!"
print(str1.startswith("Hello")) # True
print(str1.startswith("world")) # False
print(str1.startswith("o", 4)) # True,从第 4 个位置开始检索
print(str1.startswith("o", 7, 9)) # False,从第 7 个位置到第 9 个位置之间检索
输出结果为:
True
False
True
False
原文地址: https://www.cveoy.top/t/topic/ht1h 著作权归作者所有。请勿转载和采集!