mirror of
https://github.com/apachecn/ailearning.git
synced 2026-02-03 10:24:39 +08:00
132 lines
1.2 KiB
Markdown
132 lines
1.2 KiB
Markdown
# string 模块:字符串处理
|
||
|
||
In [1]:
|
||
|
||
```py
|
||
import string
|
||
|
||
```
|
||
|
||
标点符号:
|
||
|
||
In [2]:
|
||
|
||
```py
|
||
string.punctuation
|
||
|
||
```
|
||
|
||
Out[2]:
|
||
|
||
```py
|
||
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
|
||
```
|
||
|
||
字母表:
|
||
|
||
In [3]:
|
||
|
||
```py
|
||
print string.letters
|
||
print string.ascii_letters
|
||
|
||
```
|
||
|
||
```py
|
||
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
|
||
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||
|
||
```
|
||
|
||
小写和大写:
|
||
|
||
In [4]:
|
||
|
||
```py
|
||
print string.ascii_lowercase
|
||
print string.lowercase
|
||
|
||
print string.ascii_uppercase
|
||
print string.uppercase
|
||
|
||
```
|
||
|
||
```py
|
||
abcdefghijklmnopqrstuvwxyz
|
||
abcdefghijklmnopqrstuvwxyz
|
||
ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||
ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||
|
||
```
|
||
|
||
In [5]:
|
||
|
||
```py
|
||
print string.lower
|
||
|
||
```
|
||
|
||
```py
|
||
<function lower at 0x7efda4f2ae60>
|
||
|
||
```
|
||
|
||
数字:
|
||
|
||
In [6]:
|
||
|
||
```py
|
||
string.digits
|
||
|
||
```
|
||
|
||
Out[6]:
|
||
|
||
```py
|
||
'0123456789'
|
||
```
|
||
|
||
16 进制数字:
|
||
|
||
In [7]:
|
||
|
||
```py
|
||
string.hexdigits
|
||
|
||
```
|
||
|
||
Out[7]:
|
||
|
||
```py
|
||
'0123456789abcdefABCDEF'
|
||
```
|
||
|
||
每个单词的首字符大写:
|
||
|
||
In [8]:
|
||
|
||
```py
|
||
string.capwords("this is a big world")
|
||
|
||
```
|
||
|
||
Out[8]:
|
||
|
||
```py
|
||
'This Is A Big World'
|
||
```
|
||
|
||
将指定的单词放到中央:
|
||
|
||
In [9]:
|
||
|
||
```py
|
||
string.center("test", 20)
|
||
|
||
```
|
||
|
||
Out[9]:
|
||
|
||
```py
|
||
' test '
|
||
``` |