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

 



转载于:https://www.cnblogs.com/wxiaoli/p/6795273.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值