1. 程式人生 > >Python字串魔法(一)

Python字串魔法(一)

Python 字串魔法

1. expandtabs(num)

1 testStr = "name\temail\tage\nxiaohua\[email protected]\t22\nxiaoshuai\[email protected]\t23\nxiaozhang\[email protected]\t24\t"
2 print(testStr.expandtabs(15))
輸出:
1
C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\python.exe E:/Python/PyProject/Study.py
2 name email age 3 xiaohua [email protected] 22 4 xiaoshuai [email protected] 23 5 xiaozhang [email protected] 24

說明:expandtabs(num)用於查詢字串中\t(製表符)並將其剩餘長度用空格補充,如上面例子所示 num = 20,查詢到第一個\t時,前面字串name長度為4,則後面16長度將由16個空格填充。

2. isalpha()

1 testStr = "
hello world" 2 print(testStr.isalpha()) #判斷字串是否為全為字母 全字母返回true

3.isdight() , isdecimal() ,isnumeric()

testStr = "二"
print(str(testStr.isdecimal()),str(testStr.isdigit()),str(testStr.isnumeric())) 
1 # 用於判斷是否為數字 範圍: isdecimal() < isdight() < isnumeric() 
2 # 輸出結果
3 False False True

4.isprintable()

1 testStr = "hello\tworld"
2 print(testStr.isprintable()) #輸出結果為false 如果字串中在列印時的顯示與字串相同時 為true
3                              #例如"hello world" 列印時"hello world"  為真

5.isspace()

1 testStr = "hello world"
2 print(testStr.isspace()) #output: false
3                          #判斷是否全為空格

6.istitle(), title()

1 testStr = "There is a title?"
2 print(testStr.istitle())  #判斷是否為標題(所有單詞首字母大寫) Output: false
3 testStr = testStr.title() #轉換字串為標題(將所有單詞首字母大寫)
4 print(testStr.istitle())  #Output: True

7.join()

1 testStr = "你是風兒我是沙"
2 print(testStr)#output:你是風兒我是沙
3 testStr = " ".join(testStr)#將" "插入到每個字串的間隔中,""中可以是任何字元
4 print(testStr)#output: 你 是 風 兒 我 是 沙

8.center(),ljust(),rjust(),zfill()

1 testStr = "hello world"
2 print(testStr.center(20,"*")) #Output: ****hello world*****
3 print(testStr.ljust(20,"*"))  #Output: hello world*********
4 print(testStr.rjust(20,"*"))  #Output: *********hello world
5 print(testStr.zfill(20))      #Output: 000000000hello world
6                               #以指定字元填充字串 zfill只能預設填充0

9.lower(),islower(),upper().isupper()

1 testStr = "Hello World"
2 print(testStr.islower()) #Output: false  判斷是否全為小寫
3 print(testStr.lower())   #Output: hello world  全部轉換為小寫
4 print(testStr.isupper()) #Output: false  判斷是否全為大寫
5 print(testStr.upper())   #Output: HELLO WORLD  全部轉換為大寫

10.lstrip() ,rstrip(),strip()

1 testStr = " Hello World "
2 print(testStr.lstrip())  #Output: "Hello World "  預設處理字串左邊的空白(可去除\t,\n),也可指定去除的引數
3 print(testStr.rstrip())  #Output: " Hello World"  預設處理字串右邊的空白(可去除\t,\n)
4 print(testStr.strip())   #Output: "Hello World"   預設處理字串兩邊的空白(可去除\t,\n)
5 print(testStr.lstrip(" H"))#Output: " ello World"
6 print(testStr.lstrip(" Hlelo")) #Output: "World"
7 #類似一種正則表達加最大匹配的匹配規則,以" Hello World "為例,在" Hlelo"中,刪除的過程為:原Str->"ello World"->"lo World"->" World"->"world"

11.maketrans() translate()

1 Trans = str.maketrans("HeloWrd","1234567")
2 print(testStr.translate(Trans)) #Output:  12334 54637 
3 #maketrans()和tanslate()一起使用,先使用maketrans()建立一組對應關係,再使用translate()進行相應的替換,如H替換為1 e替換為2

12.partition(),rpartition(),split(),rsplit()

testStr = "qwersqwersqwersqwer"
print(testStr.partition("s")) #Output: ('qwer', 's', 'qwersqwersqwer')
print(testStr.rpartition("s"))#Output: ('qwersqwersqwer', 's', 'qwer')
print(testStr.split("s"))     #Output: ['qwer', 'qwer', 'qwer', 'qwer']
print(testStr.split("s",2))   #Output: ['qwer', 'qwer', 'qwersqwer']
print(testStr.rsplit("s"))    #Output: ['qwer', 'qwer', 'qwer', 'qwer']
print(testStr.rsplit("s",2))  #Output: ['qwersqwer', 'qwer', 'qwer']
#以上四個魔法用於分割字串,partition()優勢在於可以獲取分割符,但是不能指定分割次數,而split()可以指定分割次數,但無法獲取分割符