Python字符串基础操作
生活随笔
收集整理的這篇文章主要介紹了
Python字符串基础操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
找到我就是緣分,為技術一起努力!
格式符
# price_width = 10 item_width = width - price_widthheader_format = '%-*s%*s' format = '%-*s%*.2f'print '=' * width print header_format % (item_width, 'Item', price_width, 'Price')print '-' * widthprint 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) -1join():在字符序列之間加入分隔符
>>> 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'總結
以上是生活随笔為你收集整理的Python字符串基础操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UIPasteboard
- 下一篇: python基础教程【目录】