一,摘自官方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()至關於解凍一個元組。能夠根據須要定義