python字符串startswith_Python 字符串 startswith() 使用方法及示例
Python 字符串 startswith() 使用方法及示例
如果字符串以指定的前缀(字符串)开头,则startswith()方法将返回True。如果不是,则返回False。
startswith()的语法为:str.startswith(prefix[, start[, end]])
startswith()参数
startswith()方法最多使用三个参数:prefix -要检查的字符串或字符串元组
start(可选)- 要在字符串中检查前缀的开始位置。
end (可选)- 要在字符串中检查前缀的结束位置。
startswith()返回值
startswith()方法返回一个布尔值。如果字符串以指定的前缀开头,则返回True。
如果字符串不是以指定的前缀开头,则返回False。
示例1:startswith()没有start和end参数
示例text = "Python is easy to learn."
result = text.startswith('is easy')
# 返回 False
print(result)
result = text.startswith('Python is ')
# 返回 True
print(result)
result = text.startswith('Python is easy to learn.')
# 返回 True
print(result)
运行该程序时,输出为:False
True
True
示例2:startswith()带start和end参数
示例text = "Python programming is easy."
#起始参数: 7
# 'programming is easy.' 字符串被搜索
result = text.startswith('programming is', 7)
print(result)
# start: 7, end: 18
# 'programming' 字符串被搜索
result = text.startswith('programming is', 7, 18)
print(result)
result = text.startswith('program', 7, 18)
print(result)
运行该程序时,输出为:True
False
True
将元组传递给startswith()
在Python中可以将前缀的元组传递给startswith()方法。
如果字符串以元组的任何项目开头,则startswith()返回True。如果不是,则返回False
示例3:具有元组前缀的startswith()
示例text = "programming is easy"
result = text.startswith(('python', 'programming'))
# 输出 True
print(result)
result = text.startswith(('is', 'easy', 'java'))
# 输出 False
print(result)
# 带start 和 end 参数
# 'is easy'字符串被检查
result = text.startswith(('programming', 'easy'), 12, 19)
# 输出 False
print(result)
运行该程序时,输出为:True
False
False
如果需要检查字符串是否以指定的后缀结尾,则可以在Python中使用endswith()方法。
python字符串startswith_Python 字符串 startswith() 使用方法及示例相关推荐
- 字符串startswith_Python字符串startswith()
字符串startswith Python string startswith() function returns True if the string starts with the given p ...
- python os path isfile_Python path.isfile方法代码示例
本文整理汇总了Python中os.path.isfile方法的典型用法代码示例.如果您正苦于以下问题:Python path.isfile方法的具体用法?Python path.isfile怎么用?P ...
- python request microsoft graph_Python request.headers方法代码示例
本文整理汇总了Python中flask.request.headers方法的典型用法代码示例.如果您正苦于以下问题:Python request.headers方法的具体用法?Python reque ...
- python iteritems函数_Python six.iteritems方法代码示例
本文整理汇总了Python中sklearn.externals.six.iteritems方法的典型用法代码示例.如果您正苦于以下问题:Python six.iteritems方法的具体用法?Pyth ...
- python中的scaler_Python preprocessing.MaxAbsScaler方法代码示例
本文整理汇总了Python中sklearn.preprocessing.MaxAbsScaler方法的典型用法代码示例.如果您正苦于以下问题:Python preprocessing.MaxAbsSc ...
- python管理工具ports_Python options.port方法代码示例
本文整理汇总了Python中tornado.options.port方法的典型用法代码示例.如果您正苦于以下问题:Python options.port方法的具体用法?Python options.p ...
- python html模板_Python html.format_html方法代码示例
本文整理汇总了Python中django.utils.html.format_html方法的典型用法代码示例.如果您正苦于以下问题:Python html.format_html方法的具体用法?Pyt ...
- python session模块_Python backend.set_session方法代码示例
本文整理汇总了Python中keras.backend.set_session方法的典型用法代码示例.如果您正苦于以下问题:Python backend.set_session方法的具体用法?Pyth ...
- python get score gain_Python functional.linear方法代码示例
本文整理汇总了Python中torch.nn.functional.linear方法的典型用法代码示例.如果您正苦于以下问题:Python functional.linear方法的具体用法?Pytho ...
最新文章
- idgenerator 会重复吗_终极版:分布式唯一ID的几种生成方案
- Java ConcurrentHashMap Example and Iterator--转
- Struts2.5版本之后Tomcat启动报错问题
- ping(团队作业)
- Libra教程之:执行Transactions
- 爬取json Swaggerui界面
- centos的服务管理
- 谷歌 Fuchsia OS 进入开发者测试阶段,它真的会代替安卓吗?
- Config文件的使用:通过程序修改Config文件
- F1-VmwareCentOS7.x
- All In One For Firefox 3
- 【图像分割】基于matlab贝叶斯图像分割【含Matlab源码 1677期】
- 【推荐系统】逻辑回归(LR)在推荐系统中的使用
- 【MySQL】数据库的函数使用
- Blogspot.com再次解封
- 西门子plc200 c语言转换,西门子S7-200 系列PLC量程转换及编程方法
- 深入探究802.11ac技术
- Python常见面试题汇总(根据面试总结)
- x-data-spreadsheet 在线编辑excel文件,支持导入/导出/上传/读取网络 excel,合并单元格(vue版本)
- 芬兰本土公司Jolla计划复兴诺基亚MeeGo系统
热门文章
- python调用百度接口实现ocr识别_Python调用百度OCR实现图片文字识别的示例代码
- java aes 解密 文件_Java AES文件加解密
- java读取codetable_解析Java对象的equals()和hashCode()的使用
- Requests库基本使用
- python计算两个点之间的距离_python实现两个经纬度点之间的距离和方位角的方法...
- mvc @html.editorfor,在MVC中,@Html.EditorFor(m = ( )_CSharp_开发99编程知识库
- 【WebRTC---入门篇】(十七)实现1V1音视频实时互动直播系统
- keepalived安装与配置_Nginx_Keepalived高可用配置
- 计算机兴趣小组意义,信息技术兴趣小组开展之我见
- php strpos无效,簡單的PHP strpos功能不起作用,為什么?