Python String Method

1 common method

s为字符串
s.isalnum() 所有字符都是数字或者字母
s.isalpha() 所有字符都是字母
s.isdigit() 所有字符都是数字
s.islower() 所有字符都是小写
s.isupper() 所有字符都是大写
s.istitle() 所有单词都是首字母大写,像标题
s.isspace() 所有字符都是空白字符、 、、

s.isprintble() 可打印

>>> s = 'this is a string'
>>> s.capitalize()   #大写
'This is a string'
>>> s.upper()
'THIS IS A STRING'
>>> s.lower()
'this is a string'
>>> 'This Is A String'.swapcase()  #大小写互换
'tHIS iS a sTRING'
>>> s.find('is')



2 format method


>>> a,b = 5,42
>>> 'this is %d and that is %d'%(a,b)       #python 2

>>>'this is {0} and that is {1}'.format(a,b) #python3


>>> 'this is {0} and that is {1},this too is {1}'.format(a,b)


>>> 'this is {} and that is {}'.format(a,b) 


>>> 'this is {bob} and that is {fred}'.format(bob=a,fred=b)


>>> d=dict(bob=a,fred=b)
>>> 'this is {bob} and that is {fred}'.format(**d)



3 split  and join method


>>> s = 'This is a string of words'
>>> s.split()
['This', 'is', 'a', 'string', 'of', 'words']


>>> s.split('i')
['Th', 's ', 's a str', 'ng of words']


>>> words = s.split()

>>> for w in words:print(w)
... 
This
is
a
string
of
words


>>> new = ':' .join(words)
>>> new
'This:is:a:string:of:words'



https://docs.python.org/3.3/library/stdtypes.html#text-sequence-type-str


关于string的官方api


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值