1. 程式人生 > >Python 字串內建使用方法

Python 字串內建使用方法

4.2 字串的常用操作

  • ipython3 中定義一個 字串,例如:hello_str = ""
  • 輸入 hello_str. 按下 TAB 鍵,ipython 會提示 字串 能夠使用的 方法 如下:
In [1]: hello_str.
hello_str.capitalize    hello_str.isidentifier  hello_str.rindex
hello_str.casefold      hello_str.islower       hello_str.rjust
hello_str.center        hello_str.isnumeric     hello_str.rpartition
hello_str.count         hello_str.isprintable   hello_str.rsplit
hello_str.encode        hello_str.isspace       hello_str.rstrip
hello_str.endswith      hello_str.istitle       hello_str.split
hello_str.expandtabs    hello_str.isupper       hello_str.splitlines
hello_str.find          hello_str.join          hello_str.startswith
hello_str.format        hello_str.ljust         hello_str.strip
hello_str.format_map    hello_str.lower         hello_str.swapcase
hello_str.index         hello_str.lstrip        hello_str.title
hello_str.isalnum       hello_str.maketrans     hello_str.translate
hello_str.isalpha       hello_str.partition     hello_str.upper
hello_str.isdecimal     hello_str.replace       hello_str.zfill
hello_str.isdigit       hello_str.rfind

提示:正是因為 python 內建提供的方法足夠多,才使得在開發時,能夠針對字串進行更加靈活的操作!應對更多的開發需求!

1) 判斷型別

方法 說明
string.isspace() 如果 string 中只包含空格,則返回 True
string.isalnum() 如果 string 至少有一個字元並且所有字元都是字母或數字則返回 True
string.isalpha() 如果 string 至少有一個字元並且所有字元都是字母則返回 True
string.isdecimal() 如果 string 只包含數字則返回 True,全形數字
string.isdigit() 如果 string 只包含數字則返回 True,全形數字\u00b2
string.isnumeric() 如果 string 只包含數字則返回 True,全形數字漢字數字
string.istitle() 如果 string 是標題化的(每個單詞的首字母大寫)則返回 True
string.islower() 如果 string 中包含至少一個區分大小寫的字元,並且所有這些(區分大小寫的)字元都是小寫,則返回 True
string.isupper() 如果 string 中包含至少一個區分大小寫的字元,並且所有這些(區分大小寫的)字元都是大寫,則返回 True

2) 查詢和替換

方法 說明
string.startswith(str) 檢查字串是否是以 str 開頭,是則返回 True
string.endswith(str) 檢查字串是否是以 str 結束,是則返回 True
string.find(str, start=0, end=len(string)) 檢測 str 是否包含在 string 中,如果 start 和 end 指定範圍,則檢查是否包含在指定範圍內,如果是返回開始的索引值,否則返回 -1
string.rfind(str, start=0, end=len(string)) 類似於 find(),不過是從右邊開始查詢
string.index(str, start=0, end=len(string)) 跟 find() 方法類似,不過如果 str 不在 string 會報錯
string.rindex(str, start=0, end=len(string)) 類似於 index(),不過是從右邊開始
string.replace(old_str, new_str, num=string.count(old)) 把 string 中的 old_str 替換成 new_str,如果 num 指定,則替換不超過 num 次

3) 大小寫轉換

方法 說明
string.capitalize() 把字串的第一個字元大寫
string.title() 把字串的每個單詞首字母大寫
string.lower() 轉換 string 中所有大寫字元為小寫
string.upper() 轉換 string 中的小寫字母為大寫
string.swapcase() 翻轉 string 中的大小寫

4) 文字對齊

方法 說明
string.ljust(width) 返回一個原字串左對齊,並使用空格填充至長度 width 的新字串
string.rjust(width) 返回一個原字串右對齊,並使用空格填充至長度 width 的新字串
string.center(width) 返回一個原字串居中,並使用空格填充至長度 width 的新字串

5) 去除空白字元

方法 說明
string.lstrip() 截掉 string 左邊(開始)的空白字元
string.rstrip() 截掉 string 右邊(末尾)的空白字元
string.strip() 截掉 string 左右兩邊的空白字元

6) 拆分和連線

方法 說明
string.partition(str) 把字串 string 分成一個 3 元素的元組 (str前面, str, str後面)
string.rpartition(str) 類似於 partition() 方法,不過是從右邊開始查詢
string.split(str="", num) 以 str 為分隔符拆分 string,如果 num 有指定值,則僅分隔 num + 1 個子字串,str 預設包含 ‘\r’, ‘\t’, ‘\n’ 和空格
string.splitlines() 按照行(’\r’, ‘\n’, ‘\r\n’)分隔,返回一個包含各行作為元素的列表
string.join(seq) 以 string 作為分隔符,將 seq 中所有的元素(的字串表示)合併為一個新的字串

4.3 字串的切片

  • 切片 方法適用於 字串列表元組
    • 切片 使用 索引值 來限定範圍,從一個大的 字串切出 小的 字串
    • 列表元組 都是 有序 的集合,都能夠 通過索引值 獲取到對應的資料
    • 字典 是一個 無序 的集合,是使用 鍵值對 儲存資料