字符串startswith

Python string startswith() function returns True if the string starts with the given prefix, otherwise it returns False.

如果字符串以给定的前缀开头,则Python字符串startswith()函数将返回True ,否则返回False

Python字符串startswith() (Python String startswith())

This function syntax is:

该函数语法为:

str.startswith(prefix[, start[, end]])

The prefix can be a string or a tuple of string prefixes 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字符串以示例开头 (Python string starts with example)

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

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

s = 'Python is Awesome'# without start and end
print(s.startswith('Python'))

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

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

s = 'Python is Awesome'print(s.startswith('Python', 3))
print(s.startswith('hon', 3))

Output:

输出:

False
True

Since the start index is 3, the test will use the sub-string “hon is Awesome”. That’s why the first output is False and the second one is True.

由于起始索引为3,因此测试将使用子字符串“ hon is Awesome”。 这就是为什么第一个输出为False而第二个输出为True的原因。

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

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

s = 'Python is Awesome'print(s.startswith('is', 7, 10))
print(s.startswith('Python is', 0, 10))
print(s.startswith('Python is', 0, 6))

Output:

输出:

True
True
False

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

对于第一个print语句,子字符串为“ is”,即以“ is”开头,因此输出为True。

In the second print statement, the substring is “Python is A” and hence the output is True.

在第二个打印语句中,子字符串为“ Python is A”,因此输出为True。

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

对于第三个print语句,子字符串为“ Python”,而不以“ Python is”开头,因此输出为False。

Python字符串startswith()示例与元组 (Python string startswith() example with Tuple)

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

让我们看一些以字符串元组为前缀的示例。

s = 'Python is Awesome'print(s.startswith(('is', 'Python')))
print(s.startswith(('is', 'hon'), 7))

Output:

输出:

True
True

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

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

For the second print statement, string test begins from index position 7. So the substring is “is Awesome” which starts with “is” and hence the output as True.

对于第二个打印语句,字符串测试从索引位置7开始。因此子字符串是“ is Awesome”,以“ is”开头,因此输出为True。

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

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23821/python-string-startswith

字符串startswith

字符串startswith_Python字符串startswith()相关推荐

  1. python字符串startswith_Python 字符串 startswith() 使用方法及示例

    Python 字符串 startswith() 使用方法及示例 如果字符串以指定的前缀(字符串)开头,则startswith()方法将返回True.如果不是,则返回False. startswith( ...

  2. 【Python入门教程】第83篇 常用字符串方法之startswith()和endswith()

    本篇我们学习如何使用字符串 startswith() 方法检查一个字符串是否以某个子串开头,以及使用 endswith() 方法检查一个字符串是否以某个子串结束. 字符串 startswith() 方 ...

  3. el replace 表达式_EL表达式截取字符串 各种字符串操作的方式全解 泽0715新浪博客...

    下面是JSTL中自带的方法列表以及其描述 函数名 函数说明 使用举例 fn:contains 判定字符串是否包含另外一个字符串 fn:containsIgnoreCase 判定字符串是否包含另外一个字 ...

  4. python编写程序接收字符串_Python字符串操作

    a = 'Hello' b = 'Python' 一.字符串运算符 1.字符串连接(+) '字符串1' + '字符串2' >>>print(a + b) HelloPython 2. ...

  5. python怎么创建字符串_Python 字符串

    Python 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号( ' 或 " )来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hel ...

  6. python横向输出字符串_Python字符串及用法详解

    字符串是由数字.字母.下画线组成的一串字符,在编程语言中表示文本的数据类型.在 Python 2.X 中,普通字符串是以 8 位 ASCII 码进行存储的,而 Unicode 字符串则以 16 位 U ...

  7. 纵向输出字符串python_python字符串(转义字符)_1.05

    字符串是python中最常用的数据类型,我们可以使用引号(' 或 '')来创建字符串 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'hello word' var2 = &quo ...

  8. R语言使用str_locate函数和str_locate_all函数来定位特定字符串或者字符串模式在字符串中的位置:str_locate函数第一个位置、str_locate_all函数定位所有位置

    R语言使用str_locate函数和str_locate_all函数来定位特定字符串或者字符串模式在字符串中的位置:str_locate函数第一个位置.str_locate_all函数定位所有位置 目 ...

  9. R语言str_starts函数和str_ends函数检查在字符串的开头或者结尾是否存在特定字符串或者字符串模式

    R语言str_starts函数和str_ends函数检查在字符串的开头或者结尾是否存在特定字符串或者字符串模式 目录

最新文章

  1. 微酒店微信界面返回信息有html源码
  2. 第三章:lambda表达式
  3. Linux 文件系统 EXT4 的前世今生
  4. Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
  5. Vue结合HTML5拖放API 实现目录拖拽~
  6. java二维数组存储数据,从键盘上录入学生人数,考试科目数,以及每个学生每科分数,输出每个学生的最高分、最低分、总分、平均分
  7. 昨天考了网工,一点感想
  8. codeproject 的精彩文章----MFC系列
  9. webx rewrite
  10. Sketch for Mac(矢量绘图工具)
  11. wordpress相册_如何在WordPress中使用相册创建相册
  12. Jmeter 压测和AB压测的比较
  13. Excel调用MD5加密
  14. 决策树的预剪枝与后剪枝
  15. linux ll 命令的含义
  16. 敢死队就像看焰火表演
  17. php 采集 过滤,dedecms采集过滤规则 万能采集过滤代码 常用过滤
  18. JQuery制作3D导航栏切换动画
  19. 使用hibernate通过修改实体类文件更新数据库失效解决办法
  20. 金蝶K3WISE常用数据表(整理)

热门文章

  1. [转载] 随机游走和随机重启游走_网络动画API的随机游走
  2. verilog之按键消抖的理解
  3. 二、将mysql用作一个简单的计算器
  4. unittest和另一个可用单元测试框架nosetest
  5. VMware虚拟机 硬盘空间不足 磁盘大小调整方案
  6. 14.Xcode8imageview图片圆角不显示的bug
  7. EasyUI文档学习心得
  8. Last-Modify、ETag、Expires和Cache-Control(转载)
  9. 电脑族每天必须吃的食品
  10. php网页中不能上传图片,为什么我的PHP图片上传代码可以实现插入数据库但图片不能插入文件夹中?...