python string操作

來源:互聯網
上載者:User

標籤:python

#!/bin/python#-*- coding=utf-8 -*-import stringprint("hello,world")str1 = "       python是動態語言       "#列印str1原型print(str1)#列印去掉兩邊空格print(str1.strip())#字串大小寫轉換str2="abcd EFG,this is a TEST"print(str2.lower()) #小寫print(str2.upper()) #大寫print(str2.swapcase()) #大小寫互換print(str2.capitalize()) #首字母大寫print(string.capwords(str2)) #將字串用空格區分,再將首字母大寫print(str2.title()) #同string.capwords()print(str2.find("a",2,15)) #返回“a”所在的序號,如未找到返回-1print(str2.rfind("a")) #從右邊開始尋找print(str2.rindex("a",2,18)) #指定範圍,未找到報錯print(str2.count("E")) #統計尋找到字元的個數print(str2.replace("a","Y"))print(str1.strip().replace(" ","=")) #去掉兩邊空格print(str1.lstrip().replace(" ","=")) #去掉左邊空格print(str1.rstrip().replace(" ","=")) #去掉右邊空格str3 = "123abcEFG"str4 = "   "str5 = ""print(str3.startswith("aB")) #是否以“a”開頭,返回True或Falseprint(str3.isalnum()) #是否全為數字或字母print(str5.isdigit())#是否全為數字print(str3.isalpha()) #是否全為字母print(str4.isspace()) #是否全為空白字元print(str3.islower()) #是否全為小寫print(str3.isupper()) #是否全為大寫print(str3.istitle()) #是否為首字母大寫運行結果:hello,world       python是動態語言       python是動態語言abcd efg,this is a testABCD EFG,THIS IS A TESTABCD efg,THIS IS A testAbcd efg,this is a testAbcd Efg,this Is A TestAbcd Efg,This Is A Test-117172Ybcd EFG,this is Y TESTpython是動態語言python是動態語言==============python是動態語言FalseTrueFalseFalseTrueFalseFalseFalse


本文出自 “丁同學1990” 部落格,謝絕轉載!

python string操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.