【python】string functions

來源:互聯網
上載者:User

標籤:back   nbsp   api   find   replace   --   bsp   ccf   大寫   

1、str.replace(word0,word1)  ##用word1替換str中所有的word0

>>> ‘tea for too‘.replace(‘too‘, ‘two‘)輸出:‘tea for two‘

2、str.lower() ##字串str中的字母全部轉換成小寫

  str.upper() ##字串str中的字母全部轉換成大寫

  str.capitalize() ##字串str中的第一個字母轉換成大寫

>>> ‘df dfs ds‘.upper()‘DF DFS DS‘>>> ‘SDF SF OMP DFS‘.lower()‘sdf sf omp dfs‘
>>> ‘df dfs ds‘.capitalize()
‘Df dfs ds‘

 3. str.split(sep) ## 把str按照sep進行切分,sep預設時按照whilespace(space, tab, newline, return, formfeed)切分

>>> ‘df dfs ds‘.split()[‘df‘, ‘dfs‘, ‘ds‘]>>> ‘df,dfs,ds‘.split(‘,‘)[‘df‘, ‘dfs‘, ‘ds‘]

 

----<未完待續>---------

 

Regex通配處理

>>> import re
>>> re.findall(r‘\bf[a-z]*‘, ‘which foot or hand fell fastest‘)
輸出: [‘foot‘, ‘fell‘, ‘fastest‘]
>>> re.sub(r‘(\b[a-z]+) \1‘, r‘\1‘, ‘cat in the the hat‘)
輸出:‘cat in the hat‘

 



【python】string functions

聯繫我們

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