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

Python字符串endswith()相关推荐

  1. python字符串endswith,Python字符串| 具有示例的endswith()方法

    Python字符串endswith()方法 endswith()Method是Python中的一种库方法,用于检查字符串是否以给定的后缀(子字符串)结尾.它返回True –如果字符串以给定的后缀结尾, ...

  2. python 字符串函数_Python字符串函数

    python 字符串函数 Python provides a lot of built-in functions to manipulate strings. Python String is imm ...

  3. c int转字符串_【C++实现python字符串函数库】字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  4. Python:字符串基操_strip()/rstrip()/lstrip()_lower()/upper()_startswith()/endswith()_split()/rspilt()_join

    字符串的一些基本用法---->温故而知新,可以为师矣 Python:字符串基操_strip()/rstrip()/lstrip()_lower()/upper()_startswith()/en ...

  5. 真香!精心整理了 100+Python 字符串常用操作

    来源丨萝卜大杂烩 作者丨周萝卜 字符串作为平时使用最多的数据类型,其常用的操作我们还是很有必要熟记于心的,本文整理了多种字符串的操作的案例,还是非常用心,记得点赞收藏~ 字符串切片操作 test = ...

  6. python字符串出栈方法_python字符串常用方法

    1. isalnum() :判断字符串所有的字符都是字母或者数字.返回true和false In [1]: str1='jiangwei520' In [2]: str2='jiang wei' In ...

  7. Python字符串处理函数

    Python字符串处理函数简明 返回被去除指定字符的字符串 默认去除空白字符 删除首尾字符:str.strip([char]) 删除首字符:str.lstrip([char]) 删除尾字符str.st ...

  8. python字符串find函数-Python内置的字符串处理函数整理

    str='python String function' 生成字符串变量str='python String function' 字符串长度获取:len(str) 例:print '%s length ...

  9. Python字符串基本操作

    Python字符串基本操作 1.判断是不是合法的标识符isidentifier name="ABC" print(name.isidentifier()) 打印结果 True 2. ...

最新文章

  1. MapReduce运行原理和过程
  2. RocketMQ消息存储的整体结构
  3. C# 9 record 并非简单属性 POCO 的语法糖
  4. 十大经典排序算法1(Python版本)
  5. [vscode] 推荐插件及配置
  6. 是时候该了解一波Protocol Buffers了[Java]
  7. UIAlterController-ios8弹出菜单
  8. python difflib 编辑距离_LeetCode--072--编辑距离(python)
  9. C语言程序设计(第三版)何钦铭著 习题6-3
  10. cad黑色背景变成白色_学会了、让你前程似锦的CAD完整版教程
  11. Flask-MDict搭建在线Mdict词典服务
  12. 申请德国农工大学计算机案例,【2014】德州农工大学:腼腆男的春天
  13. Namesilo更换域名DNS解析服务器的方法
  14. 数据库设计案例(1)
  15. 微信小程序:好玩的表情包机器人
  16. 安检x光机原理计算机实现,安检X光机组成及工作原理(图文)
  17. 线上线下结合的教育模式将成为主流趋势
  18. open-api-project
  19. Python语言基础快速入门
  20. 中国式家长——旅行者困境

热门文章

  1. 网上Silverlight项目收集
  2. [转载] python 等号 重载_python运算符重载2
  3. [转载] JavaScrip ajaxt和python flask通过json传递数据的方法
  4. [转载] numpy.reshape用法(自用)
  5. [转载] python 调用自己的方法报错,numpy.ndarray object has no attribute brighten
  6. Python_Python处理JSON文件
  7. C#实现鼠标进入按键范围后按键自动窗体内位置移动
  8. 切图时图片的选择:JPG、PNG、GIF的区别
  9. Struts2学习笔记 - Action篇定义逻辑Action
  10. Flocker 做为后端存储代理 docker volume-driver 支持