字符串在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
结论
热门推荐
8 初八祝福语简短语
10 宝宝周岁可乐祝福语简短
11 许嵩生日祝福语简短
12 送笔筒的祝福语简短
13 年末祝福语简短的文案
14 回家长的祝福语简短
15 春天变冷祝福语简短
16 好听的元旦简短祝福语
17 生日祝福语大全女孩简短
18 高考前祝福语简短字句