python内置字符串处理函数_Python内置的字符串处理函数
生成字符串變量
str='python String function'
字符串長度獲取:len(str)
例:print '%s length=%d' % (str,len(str))
連接字符串
sStr1 = 'strcat'
sStr2 = 'append'
sStr1 += sStr2
print sStr1
復(fù)制字符串
#strcpy(sStr1,sStr2)
sStr1 = 'strcpy'
sStr2 = sStr1
sStr1 = 'strcpy2'
print sStr2
比較字符串
#strcmp(sStr1,sStr2)
sStr1 = 'strchr'
sStr2 = 'strch'
print cmp(sStr1,sStr2)
字符串搜索相關(guān)搜索指定字符串,沒有返回-1:str.find('t')
指定起始位置搜索:str.find('t',start)
指定起始及結(jié)束位置搜索:str.find('t',start,end)
從右邊開始查找:str.rfind('t')
搜索到多少個(gè)指定字符串:str.count('t')
上面全部方法都可用index取代。不同的是使用index查找不到會(huì)拋異常,而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())
格式化相關(guān)獲取固定長度,右對齊,左邊不夠用空格補(bǔ)齊:str.ljust(width)
獲取固定長度。左對齊,右邊不夠用空格補(bǔ)齊:str.ljust(width)
獲取固定長度,中間對齊,兩邊不夠用空格補(bǔ)齊:str.ljust(width)
獲取固定長度,右對齊,左邊不足用0補(bǔ)齊
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))
字符串替換相關(guān)替換old為new:str.replace('old','new')
替換指定次數(shù)的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為替換次數(shù)。這是替換的通用形式。另一些函數(shù)進(jìn)行特殊字符的替換
S.strip([chars]) #把S中前后chars中有的字符所有去掉,能夠理解為把S前后chars替換為None
S.lstrip([chars])
S.rstrip([chars])
S.expandtabs([tabsize])?? #把S中的tab字符替換沒空格。每一個(gè)tab替換為tabsize個(gè)空格,默認(rèn)是8個(gè)
字符串去空格及去指定字符去兩邊空格:str.strip()
去左空格:str.lstrip()
去右空格:str.rstrip()
去兩邊字符串:str.strip('d'),對應(yīng)的也有l(wèi)strip,rstrip
str=' python String function '
print '%s strip=%s' % (str,str.strip())
str='python String function'
print '%s strip=%s' % (str,str.strip('d'))
按指定字符切割字符串為數(shù)組:str.split(' ')
默認(rèn)按空格分隔str='a b c de'
print '%s strip=%s' % (str,str.split())
str='a-b-c-de'
print '%s strip=%s' % (str,str.split('-'))
字符串推斷相關(guān)是否以start開頭:str.startswith('start')
是否以end結(jié)尾:str.endswith('end')
是否全為字母或數(shù)字:str.isalnum()
是否全字母:str.isalpha()
是否全數(shù)字: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字符串與數(shù)字的轉(zhuǎn)化
數(shù)字變?yōu)樽址?str()
字符串變?yōu)閿?shù)字 string.atoi(s,[,base]) //base為進(jìn)制基數(shù)
浮點(diǎn)數(shù)轉(zhuǎn)換 string.atof(s)
字符串在輸出時(shí)的對齊:
S.ljust(width,[fillchar])?? #輸出width個(gè)字符,S左對齊,不足部分用fillchar填充,默認(rèn)的為空格。
S.rjust(width,[fillchar])??? #右對齊
S.center(width, [fillchar])??? #中間對齊
S.zfill(width)?? #把S變成width長。并在右對齊。不足部分用0補(bǔ)足
字符串中的單引號。雙引號用 \ 來轉(zhuǎn)義。
怎么把字符串轉(zhuǎn)換成數(shù)字?
int('1234')
string模塊里有
import string
>>> a="12345"
>>> import string
>>> string.atoi(a)
12345
>>> b="123.678"
>>> string.atof(b)
123.678
總結(jié)
以上是生活随笔為你收集整理的python内置字符串处理函数_Python内置的字符串处理函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue3.0中使用Element-plu
- 下一篇: websocket python爬虫_p