字符串在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 祝福语驱散霉运的话简短