python基礎學習-字串

來源:互聯網
上載者:User

標籤:大小寫   sda   style   組成   inf   exp   替換   修改   auth   

#__author:"Feng Lin"#date: 2018/8/26#字串操作s=‘sddasdw11234Wiu‘#capitalize首字母大寫s1=s.capitalize()print(s1)s2=s.upper()print(s2)s3=s.lower()print(s3)# #執行個體驗證碼不區分大小寫# s4="acEB"# you_input=input("請輸入驗證碼:")# if s4.upper()==you_input.upper():#     print("succee")# else:#     print("error")# #大小寫反轉s4=s.swapcase()print(s4)#每個隔開的單字首大寫na=‘alex egon wusier wjj‘n1=na.title()print(n1)tt=‘lin_feng*ai-nvren‘tt1=tt.title()print(tt1)s=‘alexWWEWSDAD‘#置中,預設空格填充,也可以指定填充字元s6=s.center(20)print(s6)s7=s.center(20,"$")print(s7)#自動擴充字元串中的tabs=‘alex\tsssir‘s1=s.expandtabs()print(s1)#公告方法print(len(s))#判斷是否以某個字元開頭或結尾,返回bool值s=‘bbbaaacccddd‘s8=s.startswith("b")s9=s.endswith("ddd")print(s8)print(s9)#find通過元素找索引,找不到返回-1s=‘alexWULITAOTAO‘s1=s.find(‘WULI‘)print(s1)s2=s.find(‘D‘)print(s2)#strip預設刪除前後空格s="        babablaala    "s1=s.strip()print(s1)s1=s.strip("a ")print(s1)#lstrip預設刪除左邊空格,rstrip預設刪除右邊空格s2=s.lstrip()print(s2)s3=s.rstrip()print(s3)#計算字串中元素的個數s="What Fuck!"num=s.count("AT")print(num)num=s.count("at")print(num)#自訂分割字串s="lin:feng:hao:shuai:!"l=s.split(":")print(l)#format格式化輸出三種方法s=‘my name is {},my age is {},my hobbies is {}‘.format("linfeng",24,"gril")print(s)s=‘my name is {0},my age is {1},my hobbies is {2}‘.format("linfeng",24,"gril")print(s)s=‘my name is {name},my age is {age},my hobbies is {hob}‘.format(age=24,hob="gril",name="linfeng")print(s)#replace替換字串內容,最後可以指定修改幾個s="我發誓的撒大蘇我挖到撒的阿瓦倒薩的我"s1=s.replace("我","你")print(s1)s2=s.replace("我","你",1)print(s2)#is判斷字元組成name="sadadsdads1233144"#是否只由數字組成,返回bool值print(name.isdigit())#是否只由字母組成,返回bool值print(name.isalpha())#是否由字母和數字組成,返回bool值print(name.isalnum())

 

python基礎學習-字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.