特性:不可修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
name.capitalize() 首字母大寫 name.casefold() 大寫全部變小寫 name.center( 50 , "-" ) 輸出 '---------------------Alex Li----------------------'
name.count( 'lex' ) 統計 lex出現次數
name.encode() 將字串編碼成bytes格式 name.endswith( "Li" ) 判斷字串是否以 Li結尾
"Alex\tLi" .expandtabs( 10 ) 輸出 'Alex Li' , 將\t轉換成多長的空格
name.find( 'A' ) 查詢A,找到返回其索引, 找不到返回 - 1 format :
>>> msg = "my name is {}, and age is {}"
>>> msg. format ( "alex" , 22 )
'my name is alex, and age is 22'
>>> msg = "my name is {1}, and age is {0}"
>>> msg. format ( "alex" , 22 )
'my name is 22, and age is alex'
>>> msg = "my name is {name}, and age is {age}"
>>> msg. format (age = 22 ,name = "ale" )
'my name is ale, and age is 22'
format_map >>> msg.format_map({ 'name' : 'alex' , 'age' : 22 })
'my name is alex, and age is 22'
msg.index( 'a' ) 返回a所在字串的索引
'9aA' .isalnum() True
'9' .isdigit() 是否整數
name.isnumeric name.isprintable name.isspace name.istitle name.isupper "|" .join([ 'alex' , 'jack' , 'rain' ])
'alex|jack|rain' maketrans >>> intab = "aeiou" #This is the string having actual characters.
>>> outtab = "12345" #This is the string having corresponding mapping character
>>> trantab = str .maketrans(intab, outtab)
>>>
>>> str = "this is string example....wow!!!"
>>> str .translate(trantab)
'th3s 3s str3ng 2x1mpl2....w4w!!!'
msg.partition( 'is' ) 輸出 ( 'my name ' , 'is' , ' {name}, and age is {age}' )
>>> "alex li, chinese name is lijie" .replace( "li" , "LI" , 1 )
'alex LI, chinese name is lijie'
msg.swapcase 大小寫互換
>>> msg.zfill( 40 )
'00000my name is {name}, and age is {age}' >>> n4.ljust( 40 , "-" )
'Hello 2orld-----------------------------' >>> n4.rjust( 40 , "-" )
'-----------------------------Hello 2orld' >>> b = "ddefdsdff_哈哈" >>> b.isidentifier() #檢測一段字串可否被當作標誌符,即是否符合變數命名規則
True |