a = 'abc\ndef' b = "ab'cd" c = """multiline string""" # Strings support slicing (negative index starts from the end): x = "hello"[2:4] # "ll" y = "hello"[1:-1] # "ell" z = "hello"[:4] # "hell"# Slice steps can be used, too: s = "hello"[::2] # "hlo" t = "hello"[3:0:-1] # "lle"
.elems() 方法疊代字元。例如:"bc" in "abcd" # evaluates to True x = [c for c in "abc".elems()] # x == ["a", "b", "c"]
+ 運算子。比較運算子會執行字典式比較;請使用 == 測試是否相等。
成員
- capitalize
- count
- elems
- endswith
- find
- 格式
- index
- isalnum
- isalpha
- isdigit
- islower
- isspace
- istitle
- isupper
- 加入
- lower
- lstrip
- partition
- removeprefix
- removesuffix
- replace
- rfind
- rindex
- rpartition
- rsplit
- rstrip
- split
- splitlines
- startswith
- strip
- title
- upper
大寫字母
string string.capitalize()
數量
int string.count(sub, start=0, end=None)
sub 的 (不重疊) 出現次數,可選擇限制為包含 [start:end] 和 start,但不包含 end。
          
      參數
| 參數 | 說明 | 
|---|---|
| sub | 必要 要計算的子字串。 | 
| start | int 或 None;預設值為0限制從這個位置搜尋。 | 
| end | int 或 None;
                                     預設為None可選位置,搜尋範圍會限制在這個位置之前。 | 
elems
sequence string.elems()
[s[i] for i in range(len(s))] 相同,但傳回的值可能不是清單。
        endswith
bool string.endswith(sub, start=0, end=None)
sub,則傳回 True,否則傳回 False。您可以選擇限制為 [start:end],其中 start 為包含,而 end 為排除。
          
      參數
| 參數 | 說明 | 
|---|---|
| sub | string;或 tuple 的 string;必要 要比對的字尾 (或替代字尾的元組)。 | 
| start | int 或 None;預設值為0。從這個位置開始測試。 | 
| end | int; 或 None;
                                     預設值為None停止比較的選用位置。 | 
尋找
int string.find(sub, start=0, end=None)
sub 的第一個索引,如果沒有這類索引,則傳回 -1,並可選擇限制為 [start:end],其中 start 為含頭不含尾,而 end 為不含頭不含尾。
          
      參數
| 參數 | 說明 | 
|---|---|
| sub | 必要 要尋找的子字串。 | 
| start | int 或 None;預設值為0限制從這個位置搜尋。 | 
| end | int 或 None;
                                     預設為None可選位置,搜尋範圍會限制在這個位置之前。 | 
format
string string.format(*args, **kwargs)
{} 括住的取代欄位。大括號以外的任何內容都會視為字面文字,並複製到輸出內容中,不會變更。如要在字面文字中加入大括號字元,可以將其加倍來逸出:{{ 和 }}。替代欄位可以是名稱、數字或空白。系統會使用 str 函式將值轉換為字串。# Access in order:
"{} < {}".format(4, 5) == "4 < 5"
# Access by position:
"{1}, {0}".format(2, 1) == "1, 2"
# Access by name:
"x{key}x".format(key = 2) == "x2x"參數
| 參數 | 說明 | 
|---|---|
| args | 預設值為 ()引數清單。 | 
| kwargs | 預設值為 {}引數字典。 | 
索引
int string.index(sub, start=0, end=None)
sub 的第一個索引,如果沒有這類索引,則會引發錯誤,並可選擇限制為包含 [start:end] 但不含 end。start
      參數
| 參數 | 說明 | 
|---|---|
| sub | 必要 要尋找的子字串。 | 
| start | int 或 None;預設值為0限制從這個位置搜尋。 | 
| end | int 或 None;
                                     預設為None可選位置,搜尋範圍會限制在這個位置之前。 | 
isalnum
bool string.isalnum()
isalpha
bool string.isalpha()
isdigit
bool string.isdigit()
islower
bool string.islower()
isspace
bool string.isspace()
istitle
bool string.istitle()
isupper
bool string.isupper()
彙整
string string.join(elements)
"|".join(["a", "b", "c"]) == "a|b|c"
參數
| 參數 | 說明 | 
|---|---|
| elements | 必要 要彙整的物件。 | 
低
string string.lower()
lstrip
string string.lstrip(chars=None)
chars 中出現的前置字元。請注意,chars 不是前置字串,而是會移除所有值組合:"abcba".lstrip("ba") == "cba"參數
| 參數 | 說明 | 
|---|---|
| chars | string 或 None;預設值為None要移除的字元,或 None 時的所有空白字元。 | 
分區
tuple string.partition(sep)
sep 的位置分割輸入字串,並以 (before、separator、after) 形式傳回產生的分割區,做為三元素元組。如果輸入字串不含分隔符,partition 會傳回 (self、''、'')。
          
      參數
| 參數 | 說明 | 
|---|---|
| sep | 必要 要分割的字串。 | 
removeprefix
string string.removeprefix(prefix)
prefix,則傳回移除前置字串的新字串。否則會傳回字串。
          
      參數
| 參數 | 說明 | 
|---|---|
| prefix | 必要 要移除的前置字元 (如有)。 | 
removesuffix
string string.removesuffix(suffix)
suffix,則傳回移除後綴的新字串。否則會傳回字串。
          
      參數
| 參數 | 說明 | 
|---|---|
| suffix | 必要 要移除的字尾 (如有)。 | 
取代
string string.replace(old, new, count=-1)
old 的出現次數已替換為 new,並可選擇將替換次數限制為 count。
          
      參數
| 參數 | 說明 | 
|---|---|
| old | 必要 要取代的字串。 | 
| new | 必要 要替換的字串。 | 
| count | 預設值為 -1替換次數上限。如未填寫此欄位,或值為負數,則代表沒有限制。 | 
rfind
int string.rfind(sub, start=0, end=None)
sub 的最後一個索引,如果沒有這類索引,則傳回 -1,並可選擇性地限制為包含 [start:end]、start,以及排除 end。
          
      參數
| 參數 | 說明 | 
|---|---|
| sub | 必要 要尋找的子字串。 | 
| start | int 或 None;預設值為0限制從這個位置搜尋。 | 
| end | int 或 None;
                                     預設為None可選位置,搜尋範圍會限制在這個位置之前。 | 
rindex
int string.rindex(sub, start=0, end=None)
sub 的最後一個索引,如果沒有這類索引,則會引發錯誤,並可選擇限制為包含 [start:end] 和 start,以及排除 end。
          
      參數
| 參數 | 說明 | 
|---|---|
| sub | 必要 要尋找的子字串。 | 
| start | int 或 None;預設值為0限制從這個位置搜尋。 | 
| end | int 或 None;
                                     預設為None可選位置,搜尋範圍會限制在這個位置之前。 | 
rpartition
tuple string.rpartition(sep)
sep 的位置分割輸入字串,並以 (before、separator、after) 形式傳回產生的分割區,做為三元素元組。如果輸入字串不含分隔符號,rpartition 會傳回 ('', '', self)。
          
      參數
| 參數 | 說明 | 
|---|---|
| sep | 必要 要分割的字串。 | 
rsplit
list string.rsplit(sep, maxsplit=None)
sep 做為分隔符,可選擇將分割次數限制為 maxsplit。除了從右側分割外,這個方法的行為與 split() 類似。
          
      參數
| 參數 | 說明 | 
|---|---|
| sep | 必要 要分割的字串。 | 
| maxsplit | int; 或 None;
                                     預設值為None分割次數上限。 | 
rstrip
string string.rstrip(chars=None)
chars 中出現的尾隨字元。請注意,chars 不是後置字元,而是會移除所有值組合:"abcbaa".rstrip("ab") == "abc"參數
| 參數 | 說明 | 
|---|---|
| chars | string 或 None;預設值為None要移除的字元,或 None 時的所有空白字元。 | 
斯普利特
list string.split(sep, maxsplit=None)
sep 做為分隔符,可選擇將分割次數限制為 maxsplit。
          
      參數
| 參數 | 說明 | 
|---|---|
| sep | 必要 要分割的字串。 | 
| maxsplit | int; 或 None;
                                     預設值為None分割次數上限。 | 
splitlines
sequence string.splitlines(keepends=False)
參數
| 參數 | 說明 | 
|---|---|
| keepends | 預設值為 False是否應在結果清單中加入換行符。 | 
startswith
bool string.startswith(sub, start=0, end=None)
sub 開頭,則傳回 True,否則傳回 False。您可以選擇限制為 [start:end],其中 start 為含括,而 end 為排除。
          
      參數
| 參數 | 說明 | 
|---|---|
| sub | string;或 tuple 的 string;必要 要比對的前置字串 (或替代前置字串的元組)。 | 
| start | int 或 None;預設值為0。從這個位置開始測試。 | 
| end | int 或 None;預設值為None停止比較的位置。 | 
strip
string string.strip(chars=None)
chars 中出現的前置或尾隨字元。請注意,chars 既不是前置字元也不是後置字元,系統會移除所有值組合:"aabcbcbaa".strip("ab") == "cbc"參數
| 參數 | 說明 | 
|---|---|
| chars | string 或 None;預設值為None要移除的字元,或 None 時的所有空白字元。 | 
title
string string.title()
較高
string string.upper()