string 模块:字符串处理

In [1]:

  1. import string

标点符号:

In [2]:

  1. string.punctuation

Out[2]:

  1. '!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~'

字母表:

In [3]:

  1. print string.letters
  2. print string.ascii_letters
  1. ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
  2. abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

小写和大写:

In [4]:

  1. print string.ascii_lowercase
  2. print string.lowercase
  3.  
  4. print string.ascii_uppercase
  5. print string.uppercase
  1. abcdefghijklmnopqrstuvwxyz
  2. abcdefghijklmnopqrstuvwxyz
  3. ABCDEFGHIJKLMNOPQRSTUVWXYZ
  4. ABCDEFGHIJKLMNOPQRSTUVWXYZ

In [5]:

  1. print string.lower
  1. <function lower at 0x7efda4f2ae60>

数字:

In [6]:

  1. string.digits

Out[6]:

  1. '0123456789'

16 进制数字:

In [7]:

  1. string.hexdigits

Out[7]:

  1. '0123456789abcdefABCDEF'

每个单词的首字符大写:

In [8]:

  1. string.capwords("this is a big world")

Out[8]:

  1. 'This Is A Big World'

将指定的单词放到中央:

In [9]:

  1. string.center("test", 20)

Out[9]:

  1. ' test '

原文: https://nbviewer.jupyter.org/github/lijin-THU/notes-python/blob/master/11-useful-tools/11.08-string.ipynb