Ⅰ python startwith 怎麼用
是startswith不是startwith。這是一個字元串搜索函數。判斷一個字元串是否以某某開頭。
你可以使用find(某某)==0完成相同的功能。不過startswith的可讀性更強,更容易閱讀。
相對應的就有endswith的函數,也是為了增強可讀性用的。
Ⅱ python 請問怎麼找出以$開頭的字元串,該字元串後面可能是接()+——*/=或是空字元
根據一般變數的命名規則寫了下面的程序
import re
expression="$a+$b-($c/$d) = $abc"
matchs=re.findall(r"(?<=\$)[a-z0-9]\w*",expression,re.I)
print matchs
結果是包括所有變數名字元串的列表
Ⅲ python 正則表達式,怎樣匹配以某個字元串開頭,以某個字元串結尾的情況
匹配以某個字元串開頭,以某個字元串結尾的情況的正則表達式:^abc.*?qwe$
Python正則表達式的幾種匹配用法:
1.測試正則表達式是否匹配字元串的全部或部分
regex=ur""#正則表達式
ifre.search(regex,subject):
do_something()
else:
do_anotherthing()
2.測試正則表達式是否匹配整個字元串
regex=ur"/Z"#正則表達式末尾以/Z結束
ifre.match(regex,subject):
do_something()
else:
do_anotherthing()
3.創建一個匹配對象,然後通過該對象獲得匹配細節(Create an object with details about how the regex matches (part of) a string)
regex=ur""#正則表達式
match=re.search(regex,subject)
ifmatch:
# match start:match.start()
# match end(exclusive):atch.end()
# matched text:match.group()
do_something()
else:
do_anotherthing()
4.獲取正則表達式所匹配的子串(Get the part of a string matched by the regex)
regex=ur""#正則表達式
match=re.search(regex,subject)
ifmatch:
result=match.group()
else:
result=""
5. 獲取捕獲組所匹配的子串(Get the part of a string matched by a capturing group)
regex=ur""#正則表達式
match=re.search(regex,subject)
ifmatch:
result=match.group(1)
else:
result=""
6. 獲取有名組所匹配的子串(Get the part of a string matched by a named group)
regex=ur"" #正則表達式
match = re.search(regex, subject)
if match:
result = match.group"groupname")
else:
result = ""
7. 將字元串中所有匹配的子串放入數組中(Get an array of all regex matches in a string)
result=re.findall(regex,subject)
8.遍歷所有匹配的子串(Iterate over all matches in a string)
formatchinre.finditer(r"<(.*?)/s*.*?//1>",subject)
# match start:match.start()
# match end(exclusive):atch.end()
# matched text:match.group()
9.通過正則表達式字元串創建一個正則表達式對象(Create an object to use the same regex for many operations)
reobj=re.compile(regex)
10.用法1的正則表達式對象版本(use regex object for if/else branch whether (part of) a string can be matched)
reobj=re.compile(regex)
ifreobj.search(subject):
do_something()
else:
do_anotherthing()
11.用法2的正則表達式對象版本(use regex object for if/else branch whether a string can be matched entirely)
reobj=re.compile(r"/Z")#正則表達式末尾以/Z 結束
ifreobj.match(subject):
do_something()
else:
do_anotherthing()
12.創建一個正則表達式對象,然後通過該對象獲得匹配細節(Create an object with details about how the regex object matches (part of) a string)
reobj=re.compile(regex)
match=reobj.search(subject)
ifmatch:
# match start:match.start()
# match end(exclusive):atch.end()
# matched text:match.group()
do_something()
else:
do_anotherthing()
13.用正則表達式對象獲取匹配子串(Use regex object to get the part of a string matched by the regex)
reobj=re.compile(regex)
match=reobj.search(subject)
ifmatch:
result=match.group()
else:
result=""
14.用正則表達式對象獲取捕獲組所匹配的子串(Use regex object to get the part of a string matched by a capturing group)
reobj=re.compile(regex)
match=reobj.search(subject)
ifmatch:
result=match.group(1)
else:
result=""
15.用正則表達式對象獲取有名組所匹配的子串(Use regex object to get the part of a string matched by a named group)
reobj=re.compile(regex)
match=reobj.search(subject)
ifmatch:
result=match.group("groupname")
else:
result=""
16.用正則表達式對象獲取所有匹配子串並放入數組(Use regex object to get an array of all regex matches in a string)
reobj=re.compile(regex)
result=reobj.findall(subject)
17.通過正則表達式對象遍歷所有匹配子串(Use regex object to iterate over all matches in a string)
reobj=re.compile(regex)
formatchinreobj.finditer(subject):
# match start:match.start()
# match end(exclusive):match.end()
# matched text:match.group()
Ⅳ Python中字元串無法使用endswith()函數怎麼辦
Python中字元串無法使用endswith函數,先從錯誤信息仔細看起,找到對應的位置改代碼。
根據錯誤信息反饋可知:在文件 "c: UsersABC11DesktopPython工具數字讀作.py" 中的第42行的語句if str( intn_).endswith(00):出錯,其錯誤類型是數據類型錯誤: endswith的參數必須是一個字元串或者一個字元串的tuple元組而非int。
所以,需要根據錯誤信息把第42行代碼改成if str( intn_).endswith("00"),當然根據代碼的功能判斷,錯誤遠不止這一個:以下試圖一一指出(此外,python的代碼的縮進是必須的語法結構的部分,和C語言C++Java什麼的是很不一樣的,盡量截圖python,不要直接復制粘貼,空格一被吞代碼就難看了)
以下列舉錯誤(從前往後):
①邏輯設計錯誤,在代碼的前部,input函數讀入的是字元串,num=float(input());語句將讀入的字元串變成浮點數,然後卻又把float類型的num變數使用str函數轉換類型賦值給num_。這樣做沒有語法問題,只是邏輯不通:str轉換成float再轉換回str,是否多此一舉呢;再者即使輸入是整數不帶小數點,經過str(float(input()))處理之後,結果一定會被加上小數點,那後面的if point==None:這一條件分支就完全不運行,你可以用一段小代碼驗證這一問題
64-73行修改後的代碼
Ⅳ python問題
用startswith()方法判斷一下即可
完整的Python程序如下
a=101234567890123
ifstr(a).startswith("10"):
print(1)
elifstr(a).startswith("20"):
print(2)
elifstr(a).startswith("30"):
print(3)
運行結果
1
Ⅵ python報錯提示AttributeError: 'QString' object has no attribute 'startswith'
關鍵代碼截圖來看一下
Ⅶ python 的startswith和endswith怎麼實現的啊,找到了py文件,但是裡面居然
為了回答你這個問題我專門把python源碼(github python/cpython)下載來搜了下。
首先可以確定是C語言實現的,所以在IDE只能看到聲明。
然後搜索:find . -type f -name '*.c' |xargs grep -s 'startswith'
可以看到
./Objects/bytesobject.c:bytes_startswith(PyBytesObject *self, PyObject *args)
staticPyObject*
bytes_startswith(PyBytesObject*self,PyObject*args)
{
return_Py_bytes_startswith(PyBytes_AS_STRING(self),PyBytes_GET_SIZE(self),args);
}
PyObject*
_Py_bytes_startswith(constchar*str,Py_ssize_tlen,PyObject*args)
{
return_Py_bytes_tailmatch(str,len,"startswith",args,-1);
}
/*Matchestheend(direction>=0)orstart(direction<0)ofthebuffer
*againstsubstr,usingthestartandendarguments.Returns
*-1onerror,0ifnotfoundand1iffound.
*/
staticint
tailmatch(constchar*str,Py_ssize_tlen,PyObject*substr,
Py_ssize_tstart,Py_ssize_tend,intdirection)
{
Py_buffersub_view={NULL,NULL};
constchar*sub;
Py_ssize_tslen;
if(PyBytes_Check(substr)){
sub=PyBytes_AS_STRING(substr);
slen=PyBytes_GET_SIZE(substr);
}
else{
if(PyObject_GetBuffer(substr,&sub_view,PyBUF_SIMPLE)!=0)
return-1;
sub=sub_view.buf;
slen=sub_view.len;
}
ADJUST_INDICES(start,end,len);
if(direction<0){
/*startswith*/
if(start+slen>len)
gotonotfound;
}else{
/*endswith*/
if(end-start<slen||start>len)
gotonotfound;
if(end-slen>start)
start=end-slen;
}
if(end-start<slen)
gotonotfound;
if(memcmp(str+start,sub,slen)!=0)
gotonotfound;
PyBuffer_Release(&sub_view);
return1;
notfound:
PyBuffer_Release(&sub_view);
return0;
}
Ⅷ python 正則表達式中怎麼提取並修改已匹配到的字元串
#-*-coding:utf-8-*-
__author__='lpe234'
__date__='2014-11-16'
text="""
Ihadnorealhome,nomoney,nojob,andnofriendsthatcared.
.
"""
defmain():
globaltext
text_list=text.split()
forindex,wordinenumerate(text_list):
word=word.lower()
ifword.startswith('a'):
text_list[index]=word+'hey'
text=''.join(text_list)
printtext
if__name__=='__main__':
main()
and the results:
C:Python27python.exeD:/ofshion_min_spider/scrapper/djangoo/find_a.py
Ihadnorealhome,nomoney,nojob,andheynofriendsthatcared..
Processfinishedwithexitcode0
Ⅸ 求問python的問題! def count_startswith(L, ch): ""
它那個寫法和你這個寫法是一個意思的,因為startwith函數本身就返回一個bool值,append的那一行一定要有縮進,放在if語句下面才可以。
Ⅹ python輸出有誤,出現AttributeError: 'NoneType' object has no attribute 'startswith'
你的錯誤提示並沒有看到具體是代碼中哪一行,但從提示來看,是因為某個對象沒有正常獲得數據,他們值是一個None,所以需要提前對color進行檢測。