字符串在Python中的endswith()
在本教程中,我们将学习字符串的endswith()方法。
如果字符串以给定的子字符串结尾,则endswith()方法将返回True,否则将返回False。它需要一个必需参数和两个可选参数。
必需参数是需要检查的字符串,可选参数是,它们是开始索引和结束索引。默认情况下,开始索引为0,结束索引为length-1。
示例
# initializing a string
string = "tutorialspoint"
# checking for 'point'
print(string.endswith('point'))
# checking for 'Point'
print(string.endswith('Point'))
# checking for 'tutorialspoint'
print(string.endswith('tutorialspoint'))输出结果
如果运行上面的代码,则将得到以下结果。
True False True
如果我们提供开始索引和结束索引,那么endswith()将检查这些索引之间的字符串。该方法不包括最后一个结束索引。让我们来看一个例子。
示例
# initializing a string
string = "tutorialspoint"
# checking for 'point'
print(string.endswith('point', 10, len(string)))
# checking for 'Point'
print(string.endswith('point', 8, len(string)))
# checking for 'tutorialspoint'
print(string.endswith('tutorialspoint', 0, len(string) - 1))输出结果
如果运行上面的代码,则将得到以下结果。
False True False
结论
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志