判断字符串开头结束函数
-
startswith(): 检查字符串是否是以指定子串开头,是则返回True,否则返回False,如果设置开始和结束位置下标,则在指定范围内检查
-
endswith(): 检查字符串是否是以指定子串开头,是则返回True,否则返回False,如果设置开始和结束位置下标,则在指定范围内检查
mystr = 'hello world and itcast and itheima and Python'
# startswith(): 检查字符串是否是以指定子串开头,是则返回True,否则返回False,如果设置开始和结束位置下标,则在指定范围内检查
# 语法: startswith(子串,开始位置下标,结束位置下标)
print(mystr.startswith('hello')) # True
print(mystr.startswith('hels')) # False
# endswith(): 检查字符串是否是以指定子串开头,是则返回True,否则返回False,如果设置开始和结束位置下标,则在指定范围内检查
# 语法: endswith(子串,开始位置下标,结束位置下标)
print(mystr.endswith('Python')) # True
print(mystr.endswith('hels')) # False