python字符串函数用法大全链接
startswith()函数
描述:判断字符串是否以指定字符或子字符串开头。
语法:str.endswith("suffix", start, end) 或
str[start,end].endswith("suffix") 用于判断字符串中某段字符串是否以指定字符或子字符串结尾。
—> bool 返回值为布尔类型(True,False)
- suffix — 后缀,可以是单个字符,也可以是字符串,还可以是元组("suffix"中的引号要省略)。
- start —索引字符串的起始位置。
- end — 索引字符串的结束位置。
- str.endswith(suffix) star默认为0,end默认为字符串的长度减一(len(str)-1)
注意:空字符的情况。返回值通常也为True
程序示例:
str = "hello,i love python"
print("1:",str.startswith("h"))
print("2:",str.startswith("l",2,10))# 索引 llo,i lo 是否以“n”结尾。
print("3:",str.startswith("")) #空字符
print("4:",str[0:6].startswith("h")) # 只索引 hello,
print("5:",str[0:6].startswith("e"))
print("6:",str[0:6].startswith(""))
print("7:",str.startswith(("h","z")))#遍历元组的元素,存在即返回True,否者返回False
print("8:",str.startswith(("k","m")))
程序运行结果:
1: True
2: True
3: True
4: True
5: False
6: True
7: True
8: False