1.str.capitalize() 將原字元串內的首字母轉成大寫,其他部分小寫,再返回新字元串 Output: 2.str.upper() 將原字元串的字母轉為大寫 Output: 3.str.lower() 將原字元串的字母轉為小寫 Output: 4.str.swapcase() 將原字元串內 ...
s = "abcaDa a" s2 = "123a abc ABCSAa s " s3 = "\tas \t\tb123" s4 = ' &abc123 c ## '
1.str.capitalize()
將原字元串內的首字母轉成大寫,其他部分小寫,再返回新字元串
print("s.capitalize() = {function}".format(function = s.capitalize()))
Output:
s.capitalize() = Abcada a
2.str.upper()
將原字元串的字母轉為大寫
print("s.upper() = {function}".format(function = s.upper()))
Output:
s.upper() = ABCADA A
3.str.lower()
將原字元串的字母轉為小寫
print("s.lower() = {function}".format(function = s.lower()))
Output:
s.lower() = abcada a
4.str.swapcase()
將原字元串內的大寫小寫反轉
print("s.swapcase() = {function}".format(function = s.swapcase()))
Output:
s.swapcase() = ABCAdA A
5.str.title()
原字元串內如果有特殊字元(包括數字)連接字母,則將特殊字元後的首個英文字母轉化為大寫形態,並返回新字元串
print("s2.title() = {function}".format(function = s2.title()))
Output:
s2.title() = 123A Abc Abcsaa S
6.str.center()
str.center(寬度,填充字元) 將字元串以居中的格式返回,若寬度值比len(s)小則返回原字元串,填充以從左到右為規則,填充字元的預設值為空格,值可以自己更改
print("s2.center() = {function}".format(function = s2.center(19,'&'))) print("s2.center() = {function}".format(function = s2.center(20,'&')))
Output:
#s2 = 123a abc ABCSAa s s2.center() = &123a abc ABCSAa s s2.center() = &123a abc ABCSAa s &
7.str.expandtabs()
str.expandtabs(tabsize = 8) 將原字元串中\t以前的字元補滿8位(預設),tabsize的值從0-7即8位,在0-7中任意取值則預設tabsize = 8,此後
往上+1,就相當於增加一個空格
print("s3.expandtabs ={function}".format(function = s3.expandtabs())) print("s3.expandtabs ={function}".format(function = s3.expandtabs(0))) print("s3.expandtabs ={function}".format(function = s3.expandtabs(5))) print("s3.expandtabs ={function}".format(function = s3.expandtabs(8))) print("s3.expandtabs ={function}".format(function = s3.expandtabs(9)))
Output:
#s3 = "\tas \t\tb123" s3.expandtabs = as b123 s3.expandtabs =as b123 s3.expandtabs = as b123 s3.expandtabs = as b123 s3.expandtabs = as b123
8.String_len = len(str)
公共方法,計算字元串的長度
print("s2_length = {0}".format(len(s2)))
Output:
s2_length = 18
9.str.startswith()
str.startswith(substr,strbeg,strend) 判斷原字元串是否以子字元串開頭 可以用切片的形式進行判斷(以顧頭不顧尾為原則),這裡的strend要比strbeg大否則傳回false 函數結果返回一個布爾值
print("s.startswith() = {function}".format(function = s.startswith('ab'))) print("s.startswith() = {function}".format(function = s.startswith('D',2))) print("s.startswith() = {function}".format(function = s.startswith('c',2,5))) print("s.startswith() = {function}".format(function = s.startswith('c',2,100)))
Output:
#s = abcaDa a s.startswith() = True s.startswith() = False s.startswith() = True s.startswith() = True
10.str.endswith()
str.endswith(substr,strbeg,strend) 判斷字元串是否以子字元串結尾 結果返回布爾值,這裡的strend要比strbeg小否則傳回false 和startswith()一樣要從左到右為原則
print("s.endswith() = {function}".format(function = s.endswith('Da a'))) print("s.endswith() = {function}".format(function = s.endswith('a',-1))) print("s.endswith() = {function}".format(function = s.endswith('Da a',-4))) print("s.endswith() = {function}".format(function = s.endswith('c',-8,-5))) print("s.endswith() = {function}".format(function = s.endswith('c',-7,-5))) print("s.endswith() = {function}".format(function = s.endswith('c',-7,-4))) print("s.endswith() = {function}".format(function = s.endswith('Da a',-1,6)))
Output:
#s = abcaDa a s.endswith() = True s.endswith() = True s.endswith() = True s.endswith() = True s.endswith() = True s.endswith() = False s.endswith() = False
11.str.find()
str.find(substr,strbeg,strend) 尋找字元串中是否存在該子字元串並返回其索引,找不到則返回-1 若同時出現相同的字元串則返回最先找到
的子字元串索引,切片(遵循顧頭不顧尾原則)
print("s.find() = {function}".format(function = s.find('a'))) print("s.find() = {function}".format(function = s.find('ab'))) print("s.find() = {function}".format(function = s.find('f'))) print("s.find() = {function}".format(function = s.find('b',-8,-5))) print("s.find() = {function}".format(function = s.find('b',0)))
Output:
s.find() = 0 s.find() = 0 s.find() = -1 s.find() = 1 s.find() = 1
12.str.index()
str.index(substr,strbeg,strend) 與find()的功能大致相同,但子字元串不存在於字元串中就會報錯,結果返回索引
print("s.index() = {function}".format(function = s.index('a'))) print("s.index() = {function}".format(function = s.index('ab'))) print("s.index() = {function}".format(function = s.index('D',-8,5))) print("s.index() = {function}".format(function = s.index('b',0)))
Output:
#s = abcaDa a s.index() = 0 s.index() = 0 s.index() = 4 s.index() = 1
13.str.strip()
str.strip([chars]) 去掉字元串中首和尾的字元 預設刪除的是空格 chars可以自行更改
print("s3.strip() = {function}".format(function = s3.strip()))
Output:
#s3 = as b123 s3.strip() = as b123
14.str.rstrip()
str.rstrip([chars]) 去掉字元串右邊的字元 預設刪除的是空格 返回值為刪除後的新字元串
print("s4.rstrip() = {function}".format(function = s4.rstrip())) print("s4.rstrip() = {function}".format(function = s4.rstrip('# c')))
Output:
#s4 = ' &abc123 c ## ' s4.rstrip() = &abc123 c ## s4.rstrip() = &abc123
15.str.lstrip()
str.lstrip([chars]) 去掉字元串左邊的字元 預設刪除的是空格 返回值為刪除後的新字元串
print("s4.lstrip() = {function}".format(function = s4.lstrip())) print("s4.lstrip() = {function}".format(function = s4.lstrip(' &')))
Output:
#s4 = ' &abc123 c ## ' s4.lstrip() = &abc123 c ## s4.lstrip() = abc123 c ##
16.str.count()
str.count(substr,start,end) start為第一個字元,這裡預設為0 end未結束搜索的位置,預設為len(s) 主要功能統計字元串中某個字元出現
的個數並返回個數
print("s.count() = {function}".format(function = s.count('a'))) print("s.count() = {function}".format(function = s.count('Fa'))) print("s.count() = {function}".format(function = s.count('a',-7)))
Output:
#s = "abcaDa a" s.count() = 4 s.count() = 0 s.count() = 3
17.str.split()
str.split(substr = " ",num = s.count(str)) 主要作用分割字元串 這裡的substr預設以空格分割,num為總共切割的次數,若num有數值怎分割num+1個字元串 分割後將字元串轉化為list形式 substr會在分割後消失
print("s2.split() = {function}".format(function = s2.split())) print("s2.split() = {function}".format(function = s2.split(' ',0))) print("s2.split() = {function}".format(function = s2.split('a',1))) print("s2.split() = {function}".format(function = s2.split('a',-1)))
Output:
#s2 = "123a abc ABCSAa s " s2.split() = ['123a', 'abc', 'ABCSAa', 's'] s2.split() = ['123a abc ABCSAa s '] s2.split() = ['123', ' abc ABCSAa s '] s2.split() = ['123', ' ', 'bc ABCSA', ' s ']
18.str.format()
三種用法:
print("s = {}|s2 = {}|s3 = {}".format(s,s2,s3)) print("s = {0}|s2 = {1}|s3 = {2}|s = {0}|s3 = {2}".format(s,s2,s3)) print("s = {s}{sep}s2 = {s2}{sep}s3 = {s3}".format(s = s,s2 = s2,s3 = s3,sep = '|'))
Output:
s = abcaDa a|s2 = 123a abc ABCSAa s |s3 = as b123 s = abcaDa a|s2 = 123a abc ABCSAa s |s3 = as b123|s = abcaDa a|s3 = as b123 s = abcaDa a|s2 = 123a abc ABCSAa s |s3 = as b123
當占位符不按順序寫會報錯 tuple index out of range
19.str.replace()
s.replace(old,new,max) old為將被替換的字元串,new為替換old的字元串,max為替換次數將字元串進行替換,若old未存在原字元串內則返回原
字元串
print("s.replace() = {function}".format(function = s.replace('ac','A'))) print("s.replace() = {function}".format(function = s.replace('a','A'))) print("s.replace() = {function}".format(function = s.replace('a','A',2)))
Output:
#s = "abcaDa a" s.replace() = abcaDa a s.replace() = AbcADA A s.replace() = AbcADa a
20.is函數
str.isalnum() #判斷是否由數字或字母組成
str.isalpha() #判斷是否含有字母
str.isdecimal() #判斷是否含有十進位數字
str.isdigit() #判斷是否含有數字
str.islower() #判斷是否含有小寫字母
str.isupper() #判斷是否含有大寫字母
str.isnumeric() #判斷是否只包含數字字元
str.isspace() #判斷是否只含有空格
str.istitle() #判斷是否經過title()函數處理過後的標題
21.join()
str.join(sequence) sequence為要連接的元素序列,函數作用是將指定字元串與原字元串中每一個字元一一連接
s = 'alex' print('&&'.join(s)) #a&&l&&e&&x l = ['a','l','e','x'] print('+'.join(l)) #a+l+e+x t = ('a','b') print('*'.join(t)) #a*b
PS:String索引對應