3. Python筆記 - 字串處理

3. Python筆記 - 字串處理
3-1-1: 字串處理
Python的字串能被細分成一個個字元, 並提取. 如下圖例, fruit[0:3] 為提取fruit中的第0到第2字元, 第3字元(其實是第4)是不提取的. 此處的index是從0開始算, 這是需要特別注意的. 但是如果是從後面數回來, 則是從-1開始算(不是-0).


3-1-2: Escape Sequence(跳脫序列)
在某些時候, 我們會在字串裡使用跳脫符號來避免程式誤認一些共用的符號, 如 str = "\"Mike\" is my friend.", 這時候如果要在Mike加上雙引號, 就需要跳脫符號來讓雙引號跑出來.


3-1-3: 格式化字串
格式化字串對於排版來說, 相當的方便, 並且善用格式化字串可以增進程式執行的效率, Python提供三種方式來格式化字串, 分別是 %-formatting, str().format(), f-string. 最新的是3.6版(PEP 498)提供的f-string. 下面用一些例子來說明: 
  • %-formatting: 格式化的風格其實滿好理解的, 如果是輸出字串, 使用%s; 輸出整數, 使用%d; 輸出浮點數, 使用%f. 並且在輸出類型前加上數字, 代表這個欄位要保留的長度, 預設是靠右對齊, 如果在數字前加上負號, 則改成靠左對齊. 浮點數也可以透過 .2f 或是 .4f 來調整小數的位數.
       # 範例
     import math
     PI = math.pi
    
     print("123456789012345678901234567890")
     print("%s:%f" % ("PI is", PI))
     print("%10s:%f" % ("PI is", PI))
     print("%-10s:%f" % ("PI is", PI))
     print("%-10s:%.2f" % ("PI is", PI))
     print("%-10s:%6.2f" % ("PI is", PI))
     print("%04d" % (5)) # 補0
    
     # 輸出結果
     123456789012345678901234567890
     PI is:3.141593
         PI is:3.141593
     PI is     :3.141593
     PI is     :3.14
     PI is     :  3.14
     0005
      
      
  • str().format()
       # 範例
     product, price = 'apple', 55
     print('the {} is {} dollars.'.format(product, price))
    
     # 補0, 大於符號左邊那個"0", 是要補的值, 改成其他的數字, 就會補成其他數字
     print("n的值是:{:0>05s}".format(n))
     n的值是:00005
    
     print("n的值是:{:7>05s}".format(n))
     n的值是:77775
      
      


  • f-string: 跟 %-formatting 相似, 只是將 % 改成 {} , 但需要再字串前加上 f.
       # 範例
     product, price = 'apple', 55
     test = f'the {product} is {price} dollars.'
    
     # 輸出結果
     "the apple is 55 dollars."
    
     # 補0, 方法跟 %-formatting 一樣
     n = 5
     print(f"n的值是:{n:05d}")
     n的值是:00005
      
      


3-1-4: 字串函數
將下來要介紹常用的字串函數.
(1). 對位, 排版
string.center(width)
功能: 字串置中對齊, width為整個字串的長度.

string.ljust(width)
功能: 字串置左對齊, width為整個字串的長度.

string.rjust(width)
功能: 字串置右對齊, width為整個字串的長度.

(2). string.count(sub[, start[, end]])
功能: 統計字串裡出現過 次級字串 多少次.
Ex: string.count("Apple", 20, 500)

(3). string.endswith(suffix[, start[, end]])
功能: 檢查string字串是否有符合suffix的值.
Ex: string.endswith(".png")

(4). string.find(sub[, start[, end]])
功能: 回傳sub引數的字元位置.
Ex: string.find("g")

(5). string.index(sub[, start[, end]])
功能: 與string.find()類似, 差別在於搜尋不到字串時, 會回傳ValueError錯誤訊息.

(6). 內容檢查
string.isalnum()
功能: 回傳布林值, 字串內是否只有[a-z], [A-Z], [0-9]

string.isalpha()
功能: 回傳布林值, 字串內是否只有[a-z], [A-Z]

string.isdigit()
功能: 回傳布林值, 字串內是否只有數字

string.isspace()
功能: 回傳布林值, 字串內是否為空白字元

(7). 字串大/小寫
string.islower()
功能: 回傳布林值, 字串內是否全為小寫英文字 (有數字, 空格, 其他符號不影響判斷)

string.isupper()
功能: 回傳布林值, 字串內是否全為大寫英文字 (有數字, 空格, 其他符號不影響判斷)

string.istitle()
功能: 回傳布林值, 字串第一個字是否為大寫

string.capitalize()
功能: 將字串的第一個字轉成大寫.

string.lower()
功能: 將字串的全部轉成小寫.

string.upper()
功能: 將字串的全部轉成大寫.

string.swapcase()
功能: 將字串的大小寫互轉.

(8). string.replace(old, new, count)
功能: 將 old 以 new 取代, 取代次數為count.

(9). string.zfill(width)
功能: 將字串前面補0, 直到長度等於width

最後, Python string function功能相當多, 可以參考官方提供的Guide.
Link: https://docs.python.org/2/library/string.html

Version 1 @ 12/23/2017

張貼留言

1 留言