1、capitalize()
首字母大寫。
>>> str1='hello world'
>>> str1.capitalize()
'Hello world'
>>>
2、casefold()
所有字符轉換成小寫。
>>> str2 = 'HELLO WORLD'
>>> str2.casefold()
'hello world'
>>>
3、center(width)
字符串居中顯示。
>>> str1='hello world'
>>> str2.center(30)
' HELLO WORLD '
>>>
4、count(sub[, start[, end]])
返回 sub 在字符串裏邊出現的次數,start 和 end 參數表示範圍,可選。
>>> str2 = 'HELLO WORLD'
>>> str2.count('L')
3
>>>
5、encode(encoding='utf-8', errors='strict')
以 encoding 指定的編碼格式對字符串進行編碼。
6、endswith(sub[, start[, end]])(區分大小寫)
檢查字符串是否以 sub 子字符串結束,如果是返回 True,否則返回 False。start 和 end 參數表示範圍,可選。
>>> str2 = 'HELLO WORLD'
>>> str2.endswith('d')
False
>>> str2.endswith('D')
True
>>>
7、expandtabs([tabsize=8])
把字符串中的 tab 符號(\t)轉換爲空格,如不指定參數,默認的空格數是 tabsize=8。
>>> str3 = 'hello\tworld'
>>> print(str3)
hello world
>>> print(str3.expandtabs(16))
hello world
>>>
8、find(sub[, start[, end]])(區分大小寫)
檢測 sub 是否包含在字符串中,如果有則返回索引值,否則返回 -1,start 和 end 參數表示範圍,可選。
>>> str4 = 'hello world'
>>> str4.find('l')
2
>>> str4.find('L')
-1
>>>
9、index(sub[, start[, end]])
跟 find 方法一樣,不過如果 sub 不在 string 中會產生一個異常。
>>> str4 = 'hello world'
>>> str4.index('l')
2
>>> str4.index('L')
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
str4.index('L')
ValueError: substring not found
>>>
10、isalnum()
如果字符串至少有一個字符並且所有字符都是字母或數字則返回 True,否則返回 False。
>>> str4.isalnum()
False
>>> str4
'hello world'
>>> str4 = 'helloworld'
>>> str4.isalnum()
True
>>>
>>> str4 = 'hello1234world'
>>> str4.isalnum()
True
>>>
11、isalpha()
如果字符串至少有一個字符並且所有字符都是字母則返回 True,否則返回 False。
>>> str4 = 'hello1234world'
>>> str4.isalpha()
False
>>>
>>> str4='hello world'
>>> str4.isalpha()
False
>>> str4='helloworld'
>>> str4.isalpha()
True
>>>
12、isdecimal()
如果字符串只包含十進制數字則返回 True,否則返回 False。
13、isdigit()
如果字符串只包含數字則返回 True,否則返回 False。
14、isnumeric()
如果字符串中只包含數字字符,則返回 True,否則返回 False。
s = '123' #unicode
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())
True
True
True
s = "123" # 全角
s.isdigit()
s.isdecimal()
s.isnumeric()
True
True
True
s = '三叄'
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())
False
False
True
s = 'Ⅲ'
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())
False
False
True
總結:
isdigit()
True: Unicode數字,byte數字(單字節),全角數字(雙字節)
False: 漢字數字,羅馬數字,小數
Error: 無
isdecimal()
True: Unicode數字,全角數字(雙字節)
False: 羅馬數字,漢字數字,小數
Error: byte數字(單字節)
isnumeric()
True: Unicode數字,全角數字(雙字節),羅馬數字,漢字數字
False: 小數
Error: byte數字(單字節)
15、islower()
如果字符串中至少包含一個區分大小寫的字符,並且這些字符都是小寫,則返回 True,否則返回 False。
>>> str1='Test'
>>> str1.islower()
False
>>> str1='test'
>>> str1.islower()
True
>>>
16、isspace()
如果字符串中只包含空格,則返回 True,否則返回 False。
>>> str1 = ' '
>>> str1.isspace()
True
>>> str1 = ' 1'
>>> str1.isspace()
False
>>>
17、istitle()
如果字符串是標題化(所有的單詞都是以大寫開始,其餘字母均小寫),則返回 True,否則返回 False。
>>> str1 = 'Hello World'
>>> str1.istitle()
True
>>> str1 = 'Hello world'
>>> str1.istitle()
False
>>> str1 = 'Helloworld'
>>> str1.istitle()
True
>>>
18、isupper()
如果字符串中至少包含一個區分大小寫的字符,並且這些字符都是大寫,則返回 True,否則返回 False。
>>> str1='HELLOWORLD'
>>> str1.isupper()
True
>>>
>>> str1 = 'Helloworld'
>>> str1.isupper()
False
19、join(sub)
以字符串作爲分隔符,插入到 sub 中所有的字符之間。
>>> str1.join('xyz')
'xHELLOWORLDyHELLOWORLDz'
>>>
20、ljust(width)
返回一個左對齊的字符串,並使用空格填充至長度爲 width 的新字符串。
>>> str1.ljust(30)
'HELLOWORLD '
>>>
21、lower()
轉換字符串中所有大寫字符爲小寫。
lower 和 casefolder的功能一直,區別就是lower函數只支持ascill表中的字符,而casefold則支持很多不同種類的語言。比如說β,lower職能顯示出原形而casefold則能顯示他的小寫--ss。沒有相關輸入法,沒有測試。
22、lstrip()
去掉字符串左邊的所有空格
>>> str1 = ' hello'
>>> str1
' hello'
>>> str1.lstrip()
'hello'
>>>
23、partition(sub)
找到子字符串 sub,把字符串分成一個 3 元組 (pre_sub, sub, fol_sub),如果字符串中不包含 sub 則返回 ('原字符串', '', '')
>>> str1 = 'hello world'
>>> str1.partition('o w')
('hell', 'o w', 'orld')
>>>
24、replace(old, new[, count])
把字符串中的 old 子字符串替換成 new 子字符串,如果 count 指定,則替換不超過 count 次。
>>> str1.replace('l','X')
'heXXo worXd'
>>>
>>> str1.replace('l','X',1)
'heXlo world'
>>>
25、rfind(sub[, start[, end]])
類似於 find() 方法,不過是從右邊開始查找。
26、rindex(sub[, start[, end]])
類似於 index() 方法,不過是從右邊開始。
27、rjust(width)
返回一個右對齊的字符串,並使用空格填充至長度爲 width 的新字符串。
>>> str1
'hello world'
>>> str1.rjust(30)
' hello world'
>>>
28、rpartition(sub)
類似於 partition() 方法,不過是從右邊開始查找。
29、rstrip()
刪除字符串末尾的空格。
30、split(sep=None, maxsplit=-1)
不帶參數默認是以空格爲分隔符切片字符串,如果 maxsplit 參數有設置,則僅分隔 maxsplit 個子字符串,返回切片後的子字符串拼接的列表。
>>> str1
'hello world'
>>> str1.split('o')
['hell', ' w', 'rld']
>>>
31、splitlines(([keepends]))
在輸出結果裏是否去掉換行符,默認爲 False,不包含換行符;如果爲 True,則保留換行符。。
32、startswith(prefix[, start[, end]])
檢查字符串是否以 prefix 開頭,是則返回 True,否則返回 False。start 和 end 參數可以指定範圍檢查,可選。
參照endswith。
33、strip([chars])
刪除字符串前邊和後邊所有的空格,chars 參數可以定製刪除的字符,可選。
>>> str1
' hello world '
>>> str1.strip()
'hello world'
>>> str1 = str1.strip()
>>> str1
'hello world'
>>> str1.strip('h')
'ello world'
>>>
34、swapcase()
翻轉字符串中的大小寫。
>>> str1
'Hello world'
>>> str1.swapcase()
'hELLO WORLD'
>>>
35、title()
返回標題化(所有的單詞都是以大寫開始,其餘字母均小寫)的字符串。
>>> str1
'hELLO WORLD'
>>> str1.title()
'Hello World'
>>>
36、translate(table)
根據 table 的規則(可以由 str.maketrans('a', 'b') 定製)轉換字符串中的字符。
>>> str1
'hELLO WORLD'
>>> str1.translate(str.maketrans('e','x'))
'hELLO WORLD'
>>> str1.translate(str.maketrans('E','x'))
'hxLLO WORLD'
>>>
37、upper()
轉換字符串中的所有小寫字符爲大寫。
>>> str1
'hELLO WORLD'
>>> str1.upper()
'HELLO WORLD'
>>>
38、zfill(width)
返回長度爲 width 的字符串,原字符串右對齊,前邊用 0 填充。
>>> str1
'hELLO WORLD'
>>> str1.zfill(30)
'0000000000000000000hELLO WORLD'
>>>