Python String startswith() Method
一,摘自官方API https://docs.python.org/3/library/stdtypes.html#methods
str.startswith(prefix[, start[, end]])
Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of prefixes to look for. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.
二,摘自 https://www.runoob.com/python/att-string-startswith.html
描述:
Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。
startswith()方法语法:
str.startswith(str, beg=,end=len(string))
参数:
- str -- 检测的字符串。
- strbeg -- 可选参数用于设置字符串检测的起始位置。
- strend -- 可选参数用于设置字符串检测的结束位置。
返回值:
如果检测到字符串则返回True,否则返回False。
实例:
#!/usr/bin/python str = "this is string example....wow!!!";
print str.startswith( 'this' );
print str.startswith( 'is', , );
print str.startswith( 'this', , );
结果:
True
True
False
拓展:
有多个指定开头的字符串需要判断时,可以给prefix参数传一个元组
示例:
str1 = 'a-123'
str2 = 'b-123'
str3 = 'c-123'
str4 = 'd-123' pre_list = ['a', 'b']
pre_tuple = ('a', 'b')
print(str1.startswith(tuple(pre_list)))
print(str2.startswith(pre_tuple))
print(str3.startswith(pre_tuple))
print(str4.startswith(tuple(pre_list)))
结果:
True
True
False
False
备注:tuple类型和list类型可以互转~~
元组与列表的区别在于:元组比列表的运算速度快,而且元组的数据比较安全。元组是不可改变的,为了保护其内容不被外部接口修改,不具有 append,extend,remove,pop,index这些功能;而列表是可更改的。所有有些时候我们需要两者相互转换,tuple()相当于冻结一个列表,而list()相当于解冻一个元组。可以根据需要定义
Python String startswith() Method的更多相关文章
- [Python] String strip() Method
		Description The method strip() returns a copy of the string in which all chars have been stripped fr ... 
- python string
		string比较连接 >>> s1="python string" >>> len(s) 13 >>> s2=" p ... 
- Python string objects implementation
		http://www.laurentluce.com/posts/python-string-objects-implementation/ Python string objects impleme ... 
- python string module
		String模块中的常量 >>> import string >>> string.digits ' >>> string.letters 'ab ... 
- The internals of Python string interning
		JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A fe ... 
- 在Javascript中使用String.startsWith和endsWith
		在Javascript中使用String.startsWith和endsWith 在操作字符串(String)类型的时候,startsWith(anotherString)和endsWith(anot ... 
- Python string replace   方法
		Python string replace 方法 方法1: >>> a='...fuck...the....world............' >>> b=a ... 
- String.Join Method
		Overloads Join(String, String[], Int32, Int32) Concatenates the specified elements of a string array ... 
- Python string interning原理
		原文链接:The internals of Python string interning 由于本人能力有限,如有翻译出错的,望指明. 这篇文章是讲Python string interning是如何 ... 
随机推荐
- Ubuntu安装Orcale
			Linux_Ubuntu安装oracle总结 ---------转自 https://www.2cto.com/database/201305/215338.html 话说我花了一晚上才在ubuntu ... 
- ROS常用库(五)navigation之Tutorials
			一.TF 详见古月居 https://www.guyuehome.com/355 重点:广播TF,订阅,编译时Cmakelist添加编译选项 broadcaster.sendTransform( tf ... 
- Java的SPI机制
			目录 1. 什么是SPI 2. 为什么要使用SPI 3. 关于策略模式和SPI的几点区别 4. 使用介绍或者说约定 4.1 首先介绍几个名词 4.2 约定 5. 具体的demo实现 5.1 创建服务提 ... 
- 010.Delphi插件之QPlugins,遍历服务接口
			这个DEMO注意是用来看一个DLL所拥有的全部服务接口 演示效果如下 代码如下: unit Frm_Main; interface uses Winapi.Windows, Winapi.Messag ... 
- 人脸识别  API Key和Secret Key作用
			App key简称API接口验证序号,是用于验证API接入合法性的.接入哪个网站的API接口,就需要这个网站允许才能够接入,如果简单比喻的话:可以理解成是登陆网站的用户名 App Secret简称AP ... 
- JS 选择电脑中的文件目录
			按钮调用方法function CarryOut(){ var inputObj=document.createElement('input') inputObj.setAttribute('id',' ... 
- 应用于Oculus Quest的VR头显应用
			项目需要一个VR头显项目来展示算法成果,设备为Oculus Quest一体机,基于android平台(平台要切换为android),体验了下设备效果还行,但还是有点沙窗效应.记录一下开发流程. 先贴个 ... 
- C++获取文件夹中所有文件
			获取文件夹中的文件,用到过很多次,每次用的时候都要去查下,很烦,所以想自己写下,当然,借鉴了很多其他大佬的博客 主要实现的函数,如下: void getFiles( string path, vect ... 
- MQTT 协议学习:008-在STM32上移植MQTT
			前言 通过前面了解MQTT有关概念.分析了有关的报文,我们对于这个协议也有了更深的认识.但纸上谈来终觉浅,绝知此事要躬行. 本文参考:<STM32+W5500+MQTT+Android实现远程数 ... 
- 《新标准C++程序设计》3.3-3.4(C++学习笔记7)
			1.构造函数.析构函数和变量的生存期 构造函数在对象生成时会被调用,析构函数在对象消亡时会被调用. 程序示例分析: (1) #include<iostream> using namespa ... 
