一,摘自官方API https://docs.python.org/3/library/stdtypes.html#methodshtml
str.
startswith
(prefix[, start[, end]])python
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.htmlapp
描述:this
Python startswith() 方法用于检查字符串是不是以指定子字符串开头,若是是则返回 True,不然返回 False。若是参数 beg 和 end 指定值,则在指定范围内检查。spa
startswith()方法语法:code
str.startswith(str, beg=0,end=len(string))
参数:htm
返回值:blog
若是检测到字符串则返回True,不然返回False。接口
实例:
#!/usr/bin/python str = "this is string example....wow!!!"; print str.startswith( 'this' ); print str.startswith( 'is', 2, 4 ); print str.startswith( 'this', 2, 4 );
结果:
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()至关于解冻一个元组。能够根据须要定义