python字串操作,自己留著看

2021-06-16 14:25:33 字數 1476 閱讀 9828

'vcllt'.capitalize()    #把字串的第乙個字元大寫

#'vcllt'

'lkim'.center(12,'1') #返回乙個原字串居中,使用第二個引數填充至長度12的新字串

#'1111lkim1111'

'lkim'.ljust(12,'1') #左對齊,用第二個引數填充至12長度的新字串

#'lkim11111111'

'lkim'.rjust(12,'1') #右對齊,用第二個引數填充至12長度的新字串

#'11111111lkim'

'vbsur'.count('vb',0,3) #看'vb'從第乙個字元位置到第4個字元位置,出現多少次

#1'wqvb,china'.encode('gb2312') #對字串指定編碼格式

#b'wqvb,china'

b'wqvb,china'.decode('gb2312') #對字串以指定編碼解碼

#'wqvb,china'

'doc.txt'.endswith('txt') #檢查字串的結尾是否為第乙個引數字串,第二三個引數為起始位置和結束位置

#true

'ms linux'.expandtabs(2) #將tab符號變為2個空格

#'ms linux'

'!@#$%^&*('.find('!@#$') #找到引數字串的起始位置,找不到返回-1

#0'!@#$%^&*('.index('!@#$') #和find效果相同,不過當找不到指定字串的時候會報錯

#0'a2htray'.isalnum() #有字母或者數字返回真

#true

'ashtray'.isdecimal() #只包含十進位制數字返回真

#false

'1234567890'.isdigit() #只包含數字返回真

#true

'091'.isnumeric() #字串是只含有數字返回真

#true

'keyvalue1'.isalpha() #當字串全部為字母時返回真

#false

'yes or no'.lower() #轉小寫

#'yes or no'

'yes or no'.islower() #只包含小寫字母的時候返回真

#true

'yes or no'.upper() #轉大寫

#'yes or no'

'yes or no'.isupper() #只包含大寫返回真

#true

' '.isspace() #只含有空格返回真

#true

abc = 'abc'.title() #標題化

abc.istitle() #是否標題化

#true

'asd'.join(['is','bad','boy']) #用字串連線序列

#'isasdbadasdboy'

Python字串操作

1 複製字串 str2 str1 2 鏈結字串 str abc 3 查詢字串 string.find sub string.index sub string.rfind sub string,rindex sub 4 字串比較 cmp str1,str2 cmp str1.upper str2.up...

Python字串操作

python如何判斷乙個字串只包含數字字元 python 字串比較 下面列出了常用的python實現的字串操作 strcpy sstr1,sstr2 sstr1 strcpy sstr2 sstr1 sstr1 strcpy2 print sstr2 strcat sstr1,sstr2 sstr1...

python字串操作

在 python 有各種各樣的string操作函式。在歷史上string類在 python 中經歷了一段輪迴的歷史。在最開始的時候,python 有乙個專門的string的module,要使用string的方法要先import,但後來由於眾多的 python 使用者的建議,從 python 2.0開...