Python课程笔记 Python字符串相关函数详解


字符串编码

  • GBK2312 是我国制定的中文编码,用两个字节表示中文
  • Unicode 把所有语言同一到一套编码里,不会出现乱码,两个字节表示一个字符,偏僻字用四个
  • UTF-8对全世界需要用到的字符进行了编码,是“可变长编码”,用一个字节表示字母,三个字节表示中文,还有一些是两字节或四字节。

Python3.x版本默认使用UTF-8格式编码

转义字符

在字符串前面加上’r’,或者’R’,表示后面的字符串不进行任何转义。

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
path = r'C:\windows\python.exe'
```python

### 字符串的格式化

#### % 进行格式化

##### 格式

```python
print('%d %s' % (a, b))
```python

##### 符号含义

‘-’ 指定左对齐
‘+’ 正数前面加+号
‘.n’ 指定精度

```python
>>>'%.2f' % 033333
0.33
```python

#### 格式字符

和C++基本相同,

* %d 十进制整数
* %e 以e为底的指数
* %o 八进制整数

#### format()方法进行格式化

基本语法是通过’{}’,’:‘替换之前的’%’

```python
>>>'{:%}'.format(3.5)
'350.0000%'
```python

[各类格式字符含义](https://www.runoob.com/python/att-string-format.html)

##### 指定位置参数

```python
"{}{}".format('Hello', 'World') # 按默认位置
>>> "{0}{1}{0}".format('Hello', 'World'') # 指定位置
'World Hello World’
>>> print("my name is {name}, my age is {age}, and my QQ is
{qq}".format(name = "Dong Fuguo",age = 40,qq = "30646****")) # 变量命名
my name is Dong Fuguo, my age is 40, and my QQ is 30646****
```python

##### 字符串前加f

format方法基本类似

```python
>>> name = 'Dong'
>>> age = 39
>>> f'My name is {name}, and I am {age} years old.'
'My name is Dong, and I am 39 years old.'


>>> width = 10
>>> precision = 4
>>> value = 11/3
>>> f'result:{value:{width}.{precision}}'# 可以指定宽度和精度
'result: 3.667'
```python

### 常用函数

#### find,rfind,count, index,rindex

```python
find(str,beg=0,end=len(str))
rfind(str,beg=0,end=len(str)))
index(str,beg=0,end=len(str)))
rindex(str,beg=0,end=len(str)))
count()
```python

* find()和rfind方法分别用来查找一个字符串在另一个字符串指定范围(默认是整个字符串)中首次和最后一次出现的位置,如果不存在则返回-1
* index()和rindex()方法用来返回一个字符串在另一个字符串指定范围中首次和最后一次出现的位置,如果不存在则**抛出异常**
* count()方法用来返回一个字符串在当前字符串中出现的次数

#### split()、rsplit()、partition()、rpartition()

```python
split(str="", num=string.count(str))、rsplit(str="",num=string.count(str))
```python

* split()和rsplit()方法分别用来以指定字符为分隔符,把当前字符串从左往右或从右往左分隔成多个字符串,并返回包含分隔结果的列表
* partition()和rpartition()用来以指定字符串为分隔符将原字符串**分隔为3部分**,即**分隔符前的字符串、分隔符字符串、分隔符后的字符串**,如果指定的分隔符不在原字符串中,则返回**原字符串和两个空字符串**。把字符串 string 分 成 一 个 3 元 素 的 元组(string\_pre\_str,str,string\_post\_str)

#### join

返回通过指定字符连接序列中元素后生成的新字符串。

```python
>>> li = ["apple","peach","banana","pear"]
>>> ','.join(li)
'apple,peach,banana,pear'
```python

#### lower()、upper()、capitalize()、title()、swapcase()

```python
>>> s =
"What is Your Name?"
>>> s.lower() #返回小写字符串
'what is your name?'
>>> s.upper() #返回大写字符串
'WHAT IS YOUR NAME?'
>>> s.capitalize() #字符串首字符大写'What is your name?'
>>> s.title() #每个单词的首字母大写'What Is Your Name?'
>>> s.swapcase() #大小写互换
'wHAT IS yOUR nAME?'
```python

#### replace()、maketrans()、translate()

##### replace

查找替换replace(old, new[, max]),类似于Word中的“全部替换”功能

* old – 将被替换的子字符串
* new – 新字符串,用于替换old子字符串
* max – 可选字符串, 替换不超过 max

注意:返回的是一个新字符串

```python
>>> s ="中国,中国"
>>> print(s)
中国,中国
>>> s2 = s.replace("中国","中华人民共和国")
>>> print(s2)
中华人民共和国,中华人民共和国
```python

##### maketrans()、translate()

字符串对象的maketrans()方法用来生成字符映射表,而translate()方法用来根据映射表中定义的对应关系转换字符串并替换其中的字符,使用这两个方法的组合可以同时处理多个字符。

```python
#创建映射表,将字符"abcdef123"一一对应地转换为"uvwxyz@#$"
>>> table =
''.maketrans('abcdef123','uvwxyz@#$')
>>> s ="Python is a greate programming language. I like it!"
#按映射表进行替换
>>> s.translate(table)
'Python is u gryuty progrumming lunguugy. I liky it!'
```python

一些常用的全体字母,并实现凯撒加密

```python
lower = string.ascii_lowercase #小写字母
upper = string.ascii_uppercase #大写字母
before = string.ascii_letters
after = lower[k:] + lower[:k] + upper[k:] + upper[:k]
table =''.maketrans(before, after) #创建映射表
return s.translate(table)
```python

#### strip()、rstrip()、lstrip()

```python
>>> s =" abc "
>>> s.strip() #删除空白字符
'abc'
>>> '\n\nhello world \n\n'.strip() #删除空白字符
'hello world'
>>> "aaaassddf".strip("a") #删除指定字符
'ssddf'
>>> "aaaassddf".strip("af")
'ssdd'
>>> "aaaassddfaaa".rstrip("a") #删除字符串右端指定字符
'aaaassddf'
>>> "aaaassddfaaa".lstrip("a") #删除字符串左端指定字符
'ssddfaaa'
```python

注意,这三个函数的参数指定的字符串并不作为一个整体对待,而是在原字符串的两侧、右侧、左侧删除参数字符串中包含的所有字符

```python
>>> 'aabbccddeeeffg'.strip('gbaefcd')
''
```python

#### startswith()、endswith()

s.startswith(t, beg=0,end=len(string))、s.endswith(t,beg=0,end=len(string)),判断字符串是否以指定字符串开始或结束

```python
>>> s = 'Beautiful is better than ugly.'
>>> s.startswith('Be') #检测整个字符串
True
>>> s.startswith('Be', 5) #指定检测范围起始位置
False
>>> s.startswith('Be', 0, 5) #指定检测范围起始和结束位置
True
```python

#### isalnum()、isalpha()、isdigit()、isspace()、isupper()、islower()

isalnum()、isalpha()、isdigit()、isspace()、isupper()、islower(),用来测试字符串是否为数字或字母、是否为字母、是否为数字字符、是否为空白字符、是否为大写字母以及是否为小写字母。

```python
>>> '1234abcd'.isalnum()
True
>>> '1234abcd'.isalpha() #全部为英文字母时返回True
False
>>> '1234abcd'.isdigit() #全部为数字时返回True
False
>>> 'abcd'.isalpha()
True
>>> '1234.0'.isdigit()
False
```python

#### center()、ljust()、rjust()

center()、ljust()、rjust(),返回指定宽度的新字符串,原字符串居中、左对齐或右对齐出现在新字符串中,如果指定宽度大于字符串长度,则使用指定的字符(默认为空格)进行填充。

```python
>>> 'Hello world!'.center(20) #居中对齐,以空格进行填充
' Hello world! '
>>> 'Hello world!'.center(20,'=') #居中对齐,以字符=进行填充
'====Hello world!===='
>>> 'Hello world!'.ljust(20,'=') #左对齐
'Hello world!========'
>>> 'Hello world!'.rjust(20,'=') #右对齐
'========Hello world!'
```python

### 字符串支持的运算符

#### +

生成新的字符串

```python
>>> 'hello ' + 'world'
'hello world'
```python

#### in

成员判断,关键字in

```python
>>> "a" in "abcde" #测试一个字符中是否存在于另一个字符串中
True
>>> 'ab' in 'abcde'
True
>>> 'ac' in 'abcde' #关键字in左边的字符串作为一个整体对待
False
>>> "j" in "abcde"
False
```python

#### \*

Python字符串支持与整数的乘法运算,表示序列重复,也就是字符串内容的重复,得到新字符串

```python
>>> 'abcd' * 3
'abcdabcdabcd'
```python

### 适用于字符串对象的内置函数

```python
>>> x =
'Hello world.'
>>> len(x) #字符串长度
12
>>> max(x) #最大字符
'w'
>>> min(x)
' '
>>> list(zip(x,x)) #zip()也可以作用于字符串
[('H','H'), ('e','e'), ('l','l'), ('l','l'), ('o','o'), (' ','',('w','w'), ('o','o'), ('r','r'), ('l','l'), ('d','d'), ('.','.')]
>>> sorted(x)
[' ','.','H','d','e','l','l','l','o','o','r','w']
>>> list(reversed(x))
['.','d','l','r','o','w',' ','o','l','l','e','H']
>>> list(enumerate(x))
[(0,'H'), (1,'e'), (2,'l'), (3,'l'), (4,'o'), (5,' '), (6,'w'),(7,'o'), (8,'r'), (9,'l'), (10,'d'), (11,'.')]
>>> list(map(add, x, x))
['HH','ee','ll','ll','oo',' ','ww','oo','rr','ll','dd','..']

Author: BY 水蓝
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source BY 水蓝 !
  TOC