码迷,mamicode.com
首页 > 编程语言 > 详细

【python】string functions

时间:2017-05-02 13:55:59      阅读:160      评论:0      收藏:0      [点我收藏+]

标签: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‘]

 

----<未完待续>---------

 

正则表达式通配处理

>>> 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

标签:back   nbsp   api   find   replace   --   bsp   ccf   大写   

原文地址:http://www.cnblogs.com/wxiaoli/p/6795273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!