Python内置的字符串处理函数

生成字符串变量
str='python String function'
 
字符串长度获取:len(str)
例:print '%s length=%d' % (str,len(str))

连接字符串
sStr1 = 'strcat'
sStr2 = 'append'
sStr1 += sStr2
print sStr1

复制字符串
#strcpy(sStr1,sStr2)
sStr1 = 'strcpy'
sStr2 = sStr1
sStr1 = 'strcpy2'
print sStr2

比較字符串
#strcmp(sStr1,sStr2)
sStr1 = 'strchr'
sStr2 = 'strch'
print cmp(sStr1,sStr2)

字符串搜索相关
搜索指定字符串,没有返回-1:str.find('t')
指定起始位置搜索:str.find('t',start)
指定起始及结束位置搜索:str.find('t',start,end)
从右边開始查找:str.rfind('t')
搜索到多少个指定字符串:str.count('t')
上面全部方法都可用index取代。不同的是使用index查找不到会抛异常,而find返回-1
print '%s find nono=%d' % (str,str.find('nono'))
print '%s find t=%d' % (str,str.find('t'))
print '%s find t from %d=%d' % (str,1,str.find('t',1))
print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2))
#print '%s index nono ' % (str,str.index('nono',1,2))
print '%s rfind t=%d' % (str,str.rfind('t'))
print '%s count t=%d' % (str,str.count('t'))

字母处理
所有大写:str.upper()
所有小写:str.lower()
大写和小写互换:str.swapcase()
首字母大写。其余小写:str.capitalize()
首字母大写:str.title()
print '%s lower=%s' % (str,str.lower())
print '%s upper=%s' % (str,str.upper())
print '%s swapcase=%s' % (str,str.swapcase())
print '%s capitalize=%s' % (str,str.capitalize())
print '%s title=%s' % (str,str.title()) 
格式化相关
获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width)
获取固定长度。左对齐,右边不够用空格补齐:str.ljust(width)
获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width)
获取固定长度,右对齐,左边不足用0补齐
print '%s ljust=%s' % (str,str.ljust(20))
print '%s rjust=%s' % (str,str.rjust(20))
print '%s center=%s' % (str,str.center(20))
print '%s zfill=%s' % (str,str.zfill(20))


字符串替换相关
替换old为new:str.replace('old','new')
替换指定次数的old为new:str.replace('old','new',maxReplaceTimes)
print '%s replace t to *=%s' % (str,str.replace('t', '*'))
print '%s replace t to *=%s' % (str,str.replace('t', '*',1))

S.replace(oldstr, newstr, [count])    #把S中的oldstr替换为newstr,count为替换次数。这是替换的通用形式。另一些函数进行特殊字符的替换 
S.strip([chars]) #把S中前后chars中有的字符所有去掉,能够理解为把S前后chars替换为None 
S.lstrip([chars]) 
S.rstrip([chars]) 
S.expandtabs([tabsize])   #把S中的tab字符替换没空格。每一个tab替换为tabsize个空格,默认是8个



字符串去空格及去指定字符
去两边空格:str.strip()
去左空格:str.lstrip()
去右空格:str.rstrip()
去两边字符串:str.strip('d'),对应的也有lstrip,rstrip
str=' python String function '
print '%s strip=%s' % (str,str.strip())
str='python String function'
print '%s strip=%s' % (str,str.strip('d'))

按指定字符切割字符串为数组:str.split(' ')

默认按空格分隔
str='a b c de'
print '%s strip=%s' % (str,str.split())
str='a-b-c-de'
print '%s strip=%s' % (str,str.split('-'))

字符串推断相关
是否以start开头:str.startswith('start')
是否以end结尾:str.endswith('end')
是否全为字母或数字:str.isalnum()
是否全字母:str.isalpha()
是否全数字:str.isdigit()
是否全小写:str.islower()
是否全大写:str.isupper()
str='python String function'
print '%s startwith t=%s' % (str,str.startswith('t'))
print '%s endwith d=%s' % (str,str.endswith('d'))
print '%s isalnum=%s' % (str,str.isalnum())
str='pythonStringfunction'
print '%s isalnum=%s' % (str,str.isalnum())
print '%s isalpha=%s' % (str,str.isalpha())
print '%s isupper=%s' % (str,str.isupper())
print '%s islower=%s' % (str,str.islower())
print '%s isdigit=%s' % (str,str.isdigit())
str='3423'
print '%s isdigit=%s' % (str,str.isdigit())

python字符串与数字的转化

数字变为字符串 str()
字符串变为数字 string.atoi(s,[,base]) //base为进制基数
浮点数转换 string.atof(s)


字符串在输出时的对齐:
S.ljust(width,[fillchar])   #输出width个字符,S左对齐,不足部分用fillchar填充,默认的为空格。 
S.rjust(width,[fillchar])    #右对齐 
S.center(width, [fillchar])    #中间对齐 
S.zfill(width)   #把S变成width长。并在右对齐。不足部分用0补足

 

字符串中的单引號。双引號用 来转义。

 

 怎么把字符串转换成数字?
int('1234')

string模块里有
import string
>>> a="12345"
>>> import string
>>> string.atoi(a)
12345
>>> b="123.678"
>>> string.atof(b)
123.678


原文地址:https://www.cnblogs.com/lytwajue/p/7103367.html