python字符串endswith_Python字符串endswith()

Python string endswith() function returns True if the string ends with the given suffix, otherwise it returns False.

如果字符串以给定的后缀结尾,则Python字符串endswith()函数返回True ,否则返回False 。

Python字符串endswith() (Python String endswith())

This function syntax is:

该函数语法为:

str.endswith(suffix[, start[, end]])

The suffix can be a string or a tuple of string suffixes to look for in the string.

后缀可以是字符串,也可以是要在字符串中查找的字符串后缀的元组 。

The start is an optional argument to specify the index from where test starts.

start是一个可选参数,用于指定从何处开始测试的索引。

The end is an optional argument to specify the index where the test has to stop.

end是一个可选参数,用于指定测试必须停止的索引。

Python字符串endswith()示例 (Python string endswith() example)

Let’s look at a simple example of python string endswith() function.

让我们看一个简单的python字符串endswith()函数示例。

s = 'I Love Python'

print(s.endswith('Python'))  # True

Let’s look at some examples with the start argument.

让我们来看一些带有start参数的示例。

s = 'I Love Python'

print(s.endswith('Python', 2))  # True

print(s.endswith('Python', 9))  # False

The start index is 2 for the first print statement, so the test will use the sub-string “ove Python”. That’s why the first output is True.

第一个print语句的起始索引为2,因此测试将使用子字符串“ ove Python”。 这就是为什么第一个输出为True的原因。

For the second print statement, the substring is “hon” that doesn’t end with “Python”. Hence the output is False.

对于第二个打印语句,子字符串为“ hon”,但不以“ Python”结尾。 因此,输出为False。

Let’s look at some examples with the start and end arguments.

让我们来看一些带有start和end参数的示例。

s = 'I Love Python'

print(s.endswith('hon', 7, len(s)))  # True

print(s.endswith('Pyt', 0, 10))  # True

print(s.endswith('Python', 8))  # False

For the first print statement, the substring is “Python” i.e. ending with “hon” and hence the output is True.

对于第一个打印语句,子字符串为“ Python”,即以“ hon”结尾,因此输出为True。

In the second print statement, the substring is “I Love Pyt” and hence the output is True.

在第二个打印语句中,子字符串为“ I Love Pyt”,因此输出为True。

For the third print statement, the substring is “thon” that doesn’t end with “Python”, hence the output is False.

对于第三个print语句,子字符串为“ thon”,但不以“ Python”结尾,因此输出为False。

Tuple的Python字符串endswith()示例 (Python string endswith() example with Tuple)

Let’s look at some examples with Tuple of strings as the suffix.

让我们看一些以Tuple字符串作为后缀的示例。

s = 'I Love Python'

print(s.endswith(('is', 'Python')))  # True

print(s.endswith(('Love', 'Python'), 1, 6))  # True

For the first print statement, string ends with “Python” and hence the output is True.

对于第一个打印语句,字符串以“ Python”结尾,因此输出为True。

For the second print statement, string test begins from index position 1 and ends at index position 6 (excluded). So the substring is ” Love” which ends with “Love” and hence the output as True.

对于第二条打印语句,字符串测试从索引位置1开始并在索引位置6处结束(不包括在内)。 因此,子字符串是“ Love”,以“ Love”结尾,因此输出为True。

GitHub Repository.

GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23839/python-string-endswith

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值