python 字串操作

來源:互聯網
上載者:User

標籤:python 字串操作

特性:不可修改 

name.capitalize()  首字母大寫name.casefold()   大寫全部變小寫name.center(50,"-")  輸出 ‘---------------------Alex Li----------------------‘name.count(‘lex‘) 統計 lex出現次數name.encode()  將字串編碼成bytes格式name.endswith("Li")  判斷字串是否以 Li結尾 "Alex\tLi".expandtabs(10) 輸出‘Alex      Li‘, 將\t轉換成多長的空格  name.find(‘A‘)  尋找A,找到返回其索引, 找不到返回-1 format :    >>> msg = "my name is {}, and age is {}"    >>> msg.format("alex",22)    ‘my name is alex, and age is 22‘    >>> msg = "my name is {1}, and age is {0}"    >>> msg.format("alex",22)    ‘my name is 22, and age is alex‘    >>> msg = "my name is {name}, and age is {age}"    >>> msg.format(age=22,name="ale")    ‘my name is ale, and age is 22‘format_map    >>> msg.format_map({‘name‘:‘alex‘,‘age‘:22})    ‘my name is alex, and age is 22‘msg.index(‘a‘)  返回a所在字串的索引‘9aA‘.isalnum()   True‘9‘.isdigit() 是否整數name.isnumeric  name.isprintablename.isspacename.istitlename.isupper "|".join([‘alex‘,‘jack‘,‘rain‘])‘alex|jack|rain‘maketrans    >>> intab = "aeiou"  #This is the string having actual characters.     >>> outtab = "12345" #This is the string having corresponding mapping character    >>> trantab = str.maketrans(intab, outtab)    >>>     >>> str = "this is string example....wow!!!"    >>> str.translate(trantab)    ‘th3s 3s str3ng 2x1mpl2....w4w!!!‘ msg.partition(‘is‘)   輸出 (‘my name ‘, ‘is‘, ‘ {name}, and age is {age}‘)  >>> "alex li, chinese name is lijie".replace("li","LI",1)     ‘alex LI, chinese name is lijie‘ msg.swapcase 大小寫互換 >>> msg.zfill(40)‘00000my name is {name}, and age is {age}‘>>> n4.ljust(40,"-")‘Hello 2orld-----------------------------‘>>> n4.rjust(40,"-")‘-----------------------------Hello 2orld‘>>> b="ddefdsdff_哈哈" >>> b.isidentifier() #檢測一段字串可否被當作標誌符,即是否符合變數命名規則True


本文出自 “小菜鳥” 部落格,請務必保留此出處http://baishuchao.blog.51cto.com/12918589/1935005

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.