美文网首页生活不易 我用python
Python字符串基础操作

Python字符串基础操作

作者: olaH | 来源:发表于2018-08-08 22:00 被阅读3次

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

格式符

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

相关文章

网友评论

    本文标题:Python字符串基础操作

    本文链接:https://www.haomeiwen.com/subject/cspnyftx.html