判断:
所谓判断即是判断真假,返回的结果是布尔型数据类型:True 或 False。
1、startswith():检查字符串是否是以指定⼦串开头,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。**
语法:
字符串序列.startswith(⼦串, 开始位置下标, 结束位置下标)
学习代码如下:
mystr = "hello world and itcast and itheima and Python "
# startswith() 判断字符串是否以某个子串开头
print(mystr.startswith('hello'))
print(mystr.startswith('hel'))
print(mystr.startswith('hels'))
运行结果如下:
2、endswith()::检查字符串是否是以指定⼦串结尾,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。
语法如下:
字符串序列.endswith(⼦串, 开始位置下标, 结束位置下标)
学习代码如下:
# endswith() 判断字符串是否以某个子串结尾
print(mystr.endswith('Python'))
print(mystr.endswith('hon'))
print(mystr.endswith('ton'))
运行结果如下: