首页 > 代码库 > 【python】string functions
【python】string functions
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。