Python學習日記(一) String函數使用


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索引對應

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM