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 = [s for s.elems() in "abc"] # x == ["a", "b", "c"]
+ 運算子。比較運算子會按字母順序比較;使用 == 測試是否相等。
成員
- 大寫
- 數量
- Elems
- 結尾是
- 尋找
- 格式
- 索引
- isalnum
- isalpha
- 數字
- 花粉
- isspace
- istitle
- isupper
- 加入
- 低於
- 羊皮紙
- 分區
- removeprefix
- 移除後置字串
- rfind
- rindex
- 分區
- rsplit
- 魔術橡皮擦
- 分割
- 分行符號
- 開頭為
- 條紋
- 名稱
- 大寫
大寫字母
string string.capitalize()
數量
int string.count(sub, start=0, end=None)
sub 的出現 (非重疊) 次數,可選擇限制為 [start:end],start 表示包含在內,end 則不包含。
參數
| 參數 | 說明 |
|---|---|
sub
|
必要 要計算的子字串。 |
start
|
int; or None;
預設 = 0限制搜尋這個位置。 |
end
|
int; or None;
預設值 = 無(可限定搜尋的範圍)。 |
Elms
sequence string.elems()
[s[i] for i in range(len(s))],只是傳回的值可能不是清單。
結尾為
bool string.endswith(sub, start=0, end=None)
sub 結尾,則傳回 True,否則傳回 False,否則可選擇限制為 [start:end]、start 表示包含且 end 不包含在範圍內。
參數
| 參數 | 說明 |
|---|---|
sub
|
string; or tuple of strings;
必要要比對的後置字串 (或替代後置字元的元組)。 |
start
|
int; or None;
預設 = 0從這個位置開始進行測試。 |
end
|
int; or None;
預設值 = 無可以停止比較的位置 |
尋找
int string.find(sub, start=0, end=None)
sub 的第一個索引,如果沒有這類索引,則傳回 -1,並視需要限制為 [start:end]、start 表示包含且 end 排除。
參數
| 參數 | 說明 |
|---|---|
sub
|
必要 要尋找的子字串。 |
start
|
int; or None;
預設 = 0限制搜尋這個位置。 |
end
|
int; or 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
|
default = {} 引數字典。 |
索引
int string.index(sub, start=0, end=None)
sub 的第一個索引,如果沒有這類索引,就會引發錯誤,或者是選擇將 [start:end]start 限制為包含成包含且 end 排除。
參數
| 參數 | 說明 |
|---|---|
sub
|
必要 要尋找的子字串。 |
start
|
int; or None;
預設 = 0限制搜尋這個位置。 |
end
|
int; or None;
預設值 = 無(可限定搜尋的範圍)。 |
Isalnum
bool string.isalnum()
Isalpha
bool string.isalpha()
數字
bool string.isdigit()
偏離
bool string.islower()
Isspace
bool string.isspace()
istitle
bool string.istitle()
伊斯佩普
bool string.isupper()
彙整
string string.join(elements)
"|".join(["a", "b", "c"]) == "a|b|c"
參數
| 參數 | 說明 |
|---|---|
elements
|
必要 要彙整的物件。 |
低
string string.lower()
垃圾清單
string string.lstrip(chars=None)
chars 中的前置字元的字串副本。請注意,chars 不是前置字元:系統會移除其值的所有組合:"abcba".lstrip("ba") == "cba"參數
| 參數 | 說明 |
|---|---|
chars
|
string; or None;
預設值 = 無要移除的字元,或所有空白字元 (如果 None)。 |
分區
tuple string.partition(sep)
sep 的第一個出現時分割輸入字串,並將產生的分區傳回為表單的三元素元組 (之前、分隔符、之後)。如果輸入字串不含分隔符,則會傳回分區 (self, '', '')。
參數
| 參數 | 說明 |
|---|---|
sep
|
必要 要分割的字串。 |
removeprefix
string string.removeprefix(prefix)
prefix,則會傳回已移除此前置字元的新字串。否則,系統會傳回字串。
參數
| 參數 | 說明 |
|---|---|
prefix
|
必要 要移除的前置字串 (如果有的話)。 |
移除後置字串
string string.removesuffix(suffix)
suffix,則傳回已移除後置字串的新字串。否則,系統會傳回字串。
參數
| 參數 | 說明 |
|---|---|
suffix
|
必要 要移除的後置字串 (如有)。 |
取代
string string.replace(old, new, count=-1)
old 被取代為 new,並視需要將取代次數限制為 maxsplit。
參數
| 參數 | 說明 |
|---|---|
old
|
必要 要替換的字串。 |
new
|
必要 要取代的字串。 |
count
|
預設 = -1 替換項目數量上限。如果省略此值或為負數,表示沒有限制。 |
rfind
int string.rfind(sub, start=0, end=None)
sub 的最後一個索引,如果沒有這類索引,則傳回 -1,並視需要限制為 [start:end],start 表示包含且 end 排除。
參數
| 參數 | 說明 |
|---|---|
sub
|
必要 要尋找的子字串。 |
start
|
int; or None;
預設 = 0限制搜尋這個位置。 |
end
|
int; or None;
預設值 = 無(可限定搜尋的範圍)。 |
rindex
int string.rindex(sub, start=0, end=None)
sub 的最後一個索引,如果沒有這類索引,就會引發錯誤,可選擇限制為 [start:end],start 表示包含且 end 排除。
參數
| 參數 | 說明 |
|---|---|
sub
|
必要 要尋找的子字串。 |
start
|
int; or None;
預設 = 0限制搜尋這個位置。 |
end
|
int; or None;
預設值 = 無(可限定搜尋的範圍)。 |
分區
tuple string.rpartition(sep)
sep 最後一個出現的輸入字串分割輸入字串,並將產生的分區傳回為表單的三元素元組 (之前、分隔符、之後)。如果輸入字串不含分隔符,則可傳回 '', '', 。
參數
| 參數 | 說明 |
|---|---|
sep
|
必要 要分割的字串。 |
rsplit
list string.rsplit(sep, maxsplit=None)
sep 做為分隔符,並視需要將分割次數限制為 maxsplit。除了從右側分割外,此方法的運作方式會與 Split() 一樣。
參數
| 參數 | 說明 |
|---|---|
sep
|
必要 要分割的字串。 |
maxsplit
|
int; or None;
預設值 = 無分割數量上限。 |
Rrtrip
string string.rstrip(chars=None)
chars 中結尾字元的字串副本。請注意,chars 不是後置字串,其值的所有組合都會遭到移除:"abcbaa".rstrip("ab") == "abc"參數
| 參數 | 說明 |
|---|---|
chars
|
string; or None;
預設值 = 無要移除的字元,或所有空白字元 (如果 None)。 |
斯普利特
list string.split(sep, maxsplit=None)
sep 做為分隔符,並視需要將分割次數限制為 maxsplit。
參數
| 參數 | 說明 |
|---|---|
sep
|
必要 要分割的字串。 |
maxsplit
|
int; or None;
預設值 = 無分割數量上限。 |
分段線
sequence string.splitlines(keepends=False)
參數
| 參數 | 說明 |
|---|---|
keepends
|
預設值 = 否 是否應在結果清單中納入換行符號。 |
開頭是
bool string.startswith(sub, start=0, end=None)
sub,則傳回 True,否則傳回 False,否則可選擇限制為 [start:end]、start 表示包含且 end 不包含在範圍內。
參數
| 參數 | 說明 |
|---|---|
sub
|
string; or tuple of strings;
必要要比對的前置字元 (或替代前置字元的組合)。 |
start
|
int; or None;
預設 = 0從這個位置開始進行測試。 |
end
|
int; or None;
預設值 = 無請停止比較這個位置。 |
長條
string string.strip(chars=None)
chars 中的開頭或結尾字元移除的字串副本。請注意,chars 既不是前置字元,也不是後置字串,其值的所有組合都會遭到移除:"aabcbcbaa".strip("ab") == "cbc"參數
| 參數 | 說明 |
|---|---|
chars
|
string; or None;
預設值 = 無要移除的字元,或所有空白字元 (如果 None)。 |
title
string string.title()
較高
string string.upper()