您现在的位置是:首页 > 文章详情

Python字符串基础操作

日期:2018-08-07点击:520

找到我就是缘分,为技术一起努力!

格式符

# price_width = 10 item_width = width - price_width header_format = '%-*s%*s' format = '%-*s%*.2f' print '=' * width print header_format % (item_width, 'Item', price_width, 'Price') print '-' * width print format % (item_width,'Apples',price_width,0.4) print format %(item_width,'Pears', price_width,0.5) print format %(item_width,'Cantaloupes',price_width,1.92) 
find(str,start,end):从start开始end结束寻找与str相匹配的字符,找到返回匹配项index,找不到返回-1
a='With a moo-moo here. and a moo-moo there'.find('moo',7) >>> print(a) 7 >>> a='With a moo-moo here. and a moo-moo there'.find('moo',1,8) >>> print(a) -1 
join():在字符序列之间加入分隔符
>>> seq = ['1','2','3','4','5'] >>> sep '+' >>> sep.join(seq) '1+2+3+4+5' 
lower():全部转为小写
>>> 'ZDF'.lower() 'zdf' 
title():首字母大写
>>> 'zdf'.title() 'Zdf' 
replace(oldstr , newstr):替换字符
>>> 'zdf is a clever boy!'.replace('clever','handsome') 'zdf is a handsome boy!' 
split(/PATTERN):以模式来分割,模式的意思就是REGEX
>>> 'zdf+zdf+zdf'.split('+') ['zdf', 'zdf', 'zdf'] 
strip():去除两侧空格(不包括内部的)
>>> ' zdf '.strip() 'zdf' 
strip('*'):也可指定去除两侧特定的字符 ,如*
>>> '*********zdf****************'.strip('*') 'zdf' 
原文链接:https://yq.aliyun.com/articles/637549
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章