【Python】常用的字符串函数

##python中有很多字符串函数,将经常使用的列出如下:
>>> import string
>>> s1 = 'YANGQL'
>>> s2 = 'yangql'
>>> s3 = 'yangql is learning python string'
##将字符串转换为小写
>>> s1 . lower()
'yangql'
##将字符串转换为大写
>>> s2 . upper()
'YANGQL'
##大小写互换
>>> s2 . swapcase()
'YANGQL'
>>> s7 = 'YangQL'
>>> s7 . swapcase()
'yANGql'
>>> s1 . swapcase()
'yangql'
##将首字符大写
>>> s1 . capitalize()
'Yangql'
##将字符串中所有字符串首字母大写
>>> s3 . title()
'Yangql Is Learning Python String'
s . ljust( width ,[ fillchar ])
#输出width个字符,S左对齐,不足部分用fillchar填充,默认的为空格。
>>> s2 . ljust( 10 , '#')
'yangql####'
##与s.rjust()相反
>>> s2 . rjust( 10 , '#')
'####yangql'
##使输出在中间,两边填充指定字符串
>>> s2 . center( 10 , '#')   
'##yangql##'
>>> s2 . center( 11 , '#')
'###yangql##'
##s.zfill(width) 把s变成width长,并在右对齐,不足部分用0补足
>>> s2 . zfill( 10)
'0000yangql'
#s.find(substr, [start, [end]])
#返回S中出现substr的第一个字母的标号,如果S中没有substr则返回-1。start和end作用就相当于在S[start:end]中搜索
>>> s3 . find( 'is')
7
>>> s3 . find( 'is' , 2 , 8)
- 1
>>> s3 . find( 'is' , 2 , 10)
7
>>> s3 . find( 'python')
19
##s.rfind(substr, [start, [end]])
#返回S中最后出现的substr的第一个字母的标号,如果S中没有substr则返回-1,也就是说从右边算起的第一次出现的substr的首字母标号
>>> s3 . rfind( 'python')
19
#s.count(substr, [start, [end]]) 计算substr在S中出现的次数
>>> s3 . count( 'i')
3
###把s中前后chars中有的字符全部去掉,可以理解为把S前后chars替换为None
>>> s3 . lstrip( 'yang')
'ql is learning python string'
>>> s3 . rstrip( 'string')
'yangql is learning python '
>>> s8 = '   aliyun-inc.com   '
>>> print s8
   aliyun - inc . com  
>>>
>>> s8 . strip()
'aliyun-inc.com'
#s.replace(oldstr, newstr, [count])
#把S中的oldstar替换为newstr,count为替换次数。这是替换的通用形式,还有一些函数进行特殊字符的替换
>>> s3 . replace( 'is' , 'IS')
'yangql IS learning python string'
>>>
#s.split([sep, [maxsplit]])
#以sep为分隔符,把S分成一个list。maxsplit表示分割的次数。
>>> s3 . split( 'i')
[ 'yangql ' , 's learn' , 'ng python str' , 'ng' ]
##rsplit()从前面查询分割点,split()从字符串后面查询。
>>> s3 . rsplit( 'i' , 2)
[ 'yangql is learn' , 'ng python str' , 'ng' ]
>>> s3 . rsplit( 'i' , 3)
[ 'yangql ' , 's learn' , 'ng python str' , 'ng' ]
>>> s3 . split( 'i' , 3)
[ 'yangql ' , 's learn' , 'ng python str' , 'ng' ]
>>> s3 . split( 'i' , 2)
[ 'yangql ' , 's learn' , 'ng python string' ]
>>> s3 . split() ## 默认的分割符为空白字符   
[ 'yangql' , 'is' , 'learning' , 'python' , 'string' ]
>>> s3 . rsplit( 'i' , 2)
[ 'yangql is learn' , 'ng python str' , 'ng' ]
>>> s3 . rsplit( 'i' , 1)
[ 'yangql is learning python str' , 'ng' ]
>>> s3 . split( 'i' , 1)
[ 'yangql ' , 's learning python string' ]
>>>
>>> s3 . join( s2 [ 1 ])
'a'
#s4.join(s2)函数将s2分开,并每个字母接上s4的字符
>>> s4 = 'python '
>>> s4 . join( 's')        
's'
>>> s4 . join( 'us')
'upythons'
>>> s4 . join( s2)
'ypython apython npython gpython qpython l'
>>> s4 . join( s2 [ 0 ])
'y'
##将unicode转换为utf-8类型的字符串
>>> s4 = u'python '
>>> type( s4)
< type 'unicode' >
>>> s5 = s4 . encode( 'utf-8')
>>> type( s5)
< type 'str' >
>>> type( s4 . encode( 'utf-8'))
< type 'str' >
>>>
>>>
>>> s6 = '123456'
>>> s7 = 'ss124'
>>> s2 . isalnum()
True
>>> s2 . isalpha()
True
>>> s2
'yangql'
>>> s2 . isdigit()
False

s . isalnum()
#是否全是字母和数字,并至少有一个字符
s . isalpha() #是否全是字母,并至少有一个字符
s6 = '123456'
>>> s1 . isalpha()
True
>>> s6 . isalpha()
False
>>>
s . isdigit() #是否全是数字,并至少有一个字符
>>> s6 . isdigit()
True
#s.isspace() 是否全是空白字符,并至少有一个字符
>>> s6 . isspace()
False
#s.islower() S中的字母是否全是小写
>>> s6 . islower()
False
>>> s2 . islower()
True
#s.isupper() S中的字母是否便是大写
>>> s2 . isupper()
False
>>> s1 . isupper()
True
>>>
s . istitle() #S是否是首字母大写的
>>>
>>> s2 . istitle()
False
##转换字符串为数据
>>> string . atof( s6)
123456.0
>>> string . atoi( s6)
123456
>>> string . atol( s6)
123456L
>>>

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22664653/viewspace-713251/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/22664653/viewspace-713251/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值