1.字符串屬性方法操做:python
1.>字符串格式輸出對齊git
1
2
3
4
5
6
7
8
9
10
11
|
>>>
str
=
"Python stRING"
>>>
print
str
.center(
20
)
#生成20個字符長度,str排中間
Python stRING
>>>
print
str
.ljust(
20
)
#生成20個字符長度,str左對齊
Python stRING
>>>
print
str
.rjust(
20
)
#生成20個字符長度,str右對齊
Python stRING
|
2.>大小寫轉換api
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
>>>
str
=
"Python stRING"
>>>
str
.upper()
#轉大寫
'PYTHON STRING'
>>>
str
.lower()
#轉小寫
'python string'
>>>
str
.capitalize()
#字符串首爲大寫,其他小寫
'Python string'
>>>
str
.swapcase()
#大小寫對換
'pYTHON STring'
>>>
str
.title()
#以分隔符爲標記,首字符爲大寫,其他爲小寫
'Python String'
|
3.>字符串條件判斷學習
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
|
>>>
str
=
'01234'
>>>
str
.isalnum()
#是否全是字母和數字,並至少有一個字符
True
>>>
str
.isdigit()
#是否全是數字,並至少有一個字符
True
>>>
str
=
'string'
>>>
str
.isalnum()
#是否全是字母和數字,並至少有一個字符
True
>>>
str
.isalpha()
#是否全是字母,並至少有一個字符
True
>>>
str
.islower()
#是否全是小寫,當全是小寫和數字一塊兒時候,也判斷爲True
True
>>>
str
=
"01234abcd"
>>>
str
.islower()
#是否全是小寫,當全是小寫和數字一塊兒時候,也判斷爲True
True
>>>
str
.isalnum()
#是否全是字母和數字,並至少有一個字符
True
>>>
str
=
' '
>>>
str
.isspace()
#是否全是空白字符,並至少有一個字符
True
>>>
str
=
'ABC'
>>>
str
.isupper()
#是否全是大寫,當全是大寫和數字一塊兒時候,也判斷爲True
True
>>>
str
=
'Aaa Bbb'
>>>
str
.istitle()
#全部單詞字首都是大寫,標題
True
>>>
str
=
'string learn'
>>>
str
.startswith(
'str'
)
#判斷字符串以'str'開頭
True
>>>
str
.endswith(
'arn'
)
#判讀字符串以'arn'結尾
True
|
4.>字符串搜索定位與替換編碼
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
|
>>>
str
=
'string lEARn'
>>>
str
.find(
'z'
)
#查找字符串,沒有則返回-1,有則返回查到到第一個匹配的索引
-
1
>>>
str
.find(
'n'
)
#返回查到到第一個匹配的索引
4
>>>
str
.rfind(
'n'
)
#返回的索引是最後一次匹配的
11
>>>
str
.index(
'a'
)
#若是沒有匹配則報錯
Traceback (most recent call last):
File
"<input>"
, line
1
,
in
<module>
ValueError: substring
not
found
>>>
str
.index(
"n"
)
#同find相似,返回第一次匹配的索引值
4
>>>
str
.rindex(
"n"
)
#返回最後一次匹配的索引值
11
>>>
str
.count(
'a'
)
#字符串中匹配的次數
0
>>>
str
.count(
'n'
)
#同上
2
>>>
str
.replace(
'EAR'
,
'ear'
)
#匹配替換
'string learn'
>>>
str
.replace(
'n'
,
'N'
)
'striNg lEARN'
>>>
str
.replace(
'n'
,
'N'
,
1
)
'striNg lEARn'
>>>
str
.strip(
'n'
)
#刪除字符串首尾匹配的字符,一般用於默認刪除回車符
'string lEAR'
>>>
str
.lstrip(
'n'
)
#左匹配
'string lEARn'
>>>
str
.rstrip(
'n'
)
#右匹配
'string lEAR'
>>>
str
=
" tab"
>>>
str
.expandtabs()
#把製表符轉爲空格
' tab'
>>>
str
.expandtabs(
2
)
#指定空格數
' tab'
|
5.>字符串編碼與解碼spa
1
2
3
4
5
6
7
8
9
10
11
12
|
>>>
str
=
"字符串學習"
>>>
str
'\xe5\xad\x97\xe7\xac\xa6\xe4\xb8\xb2\xe5\xad\xa6\xe4\xb9\xa0'
>>>
str
.decode(
'utf-8'
)
#解碼過程,將utf-8解碼爲unicode
u
'\u5b57\u7b26\u4e32\u5b66\u4e60'
>>>
str
.decode(
"utf-8"
).encode(
'gbk'
)
#編碼過程,將unicode編碼爲gbk
'\xd7\xd6\xb7\xfb\xb4\xae\xd1\xa7\xcf\xb0'
>>>
str
.decode(
'utf-8'
).encode(
'utf-8'
)
#將unicode編碼爲utf-8
'\xe5\xad\x97\xe7\xac\xa6\xe4\xb8\xb2\xe5\xad\xa6\xe4\xb9\xa0'
|
6.>字符串分割變換code
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
|
>>
str
=
"Learn string"
>>>
'-'
.join(
str
)
'L-e-a-r-n- -s-t-r-i-n-g'
>>> li
=
[
'Learn'
,
'string'
]
>>>
'-'
.join(li)
'Learn-string'
>>>
str
.split(
'n'
)
[
'Lear'
,
' stri'
,
'g'
]
>>>
str
.split(
'n'
,
1
)
[
'Lear'
,
' string'
]
>>>
str
.rsplit(
'n'
)
[
'Lear'
,
' stri'
,
'g'
]
>>>
str
.rsplit(
'n'
,
1
)
[
'Learn stri'
,
'g'
]
>>>
str
.splitlines()
[
'Learn string'
]
>>>
str
.partition(
'n'
)
(
'Lear'
,
'n'
,
' string'
)
>>>
str
.rpartition(
'n'
)
(
'Learn stri'
,
'n'
,
'g'
)
|