1. 程式人生 > >Python:字符串函數

Python:字符串函數

進行 字符 abc tag 數字0 pan 字符串替換 () 返回

String模塊中的常量:

string.digits:數字0~9

string.letters:所有字母(大小寫)

string.lowercase:所有小寫字母

string.printable:可打印字符的字符串

string.punctuation:所有標點

string.uppercase:所有大寫字母

[html] view plain copy
  1. >>> import string
  2. >>> string.digits
  3. ‘0123456789‘
  4. >>> string.letters
  5. ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz‘
  6. >>> string.lowercase
  7. ‘abcdefghijklmnopqrstuvwxyz‘
  8. >>> string.printable
  9. ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\‘()*+,-./:;<=>[email protected][\\]^_`{|}~ \t\n\r\x0b\x0c‘
  10. >>> string.punctuation
  11. ‘!"#$%&\‘()*+,-./:;<=>[email protected][\\]^_`{|}~‘
  12. >>> string.uppercase
  13. ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘

1、find函數

在一個較長的字符串中查詢子字符串,返回子串所在位置最左端索引,沒有找到返回-1

[html] view plain copy
  1. >>> title = "Monty Python‘s Flying Circus"
  2. >>> title.find(‘Monty‘)
  3. 0
  4. >>> title.find(‘monty‘)
  5. -1
可以選擇起始點和結束點

[html] view plain copy
  1. >>> title.find(‘Python‘)
  2. 6
  3. >>> title.find(‘Python‘, 3)
  4. 6
  5. >>> title.find(‘Python‘, 3, 10)
  6. -1

2、join函數

在隊列中添加元素(只能操作於字符串,返回一個修改後的字符串,但是原字符串不改變)

[html] view plain copy
  1. >>> seq = [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]
  2. >>> sep = ‘+‘
  3. >>> sep.join(seq)
  4. ‘1+2+3+4+5‘
  5. >>> seq
  6. [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]
  7. >>> dirs = ‘‘, ‘usr‘, ‘bin‘, ‘env‘
  8. >>> ‘/‘.join(dirs)
  9. ‘/usr/bin/env‘
  10. >>> print ‘C:‘ + ‘\\‘.join(dirs)
  11. C:\usr\bin\env

逆方法:split函數

將字符串分割成序列,返回該序列,原字符串不改變

[html] view plain copy
  1. >>> word = ‘1+2+3+4+5‘
  2. >>> word.split(‘+‘)
  3. [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]
  4. >>> word
  5. ‘1+2+3+4+5‘

3、lower函數

返回字符串的小寫字母版

[html] view plain copy
  1. >>> ‘fafDAWdfaweDWED‘.lower()
  2. ‘fafdawdfawedwed‘
擴展:

title函數:首字母大寫,其他小寫

[html] view plain copy
  1. >>> "that‘s all folks".title()
  2. "That‘S All Folks"
capwords函數:功能同上,為string模塊中函數

[html] view plain copy
  1. >>> import string
  2. >>> string.capwords("that‘s all folks")
  3. "That‘s All Folks"

4、replace函數

返回某字符串所有匹配項均被替換之後得到的字符串,原字符串不改變

[html] view plain copy
  1. >>> word = ‘this is a test‘
  2. >>> word.replace(‘is‘, ‘eez‘)
  3. ‘theez eez a test‘
  4. >>> word
  5. ‘this is a test‘

maketrans函數:功能同上,string中的轉換表,共有256個項目,函數接受2個等長的字符串,第一個字符串中的每個字符都用第二個字符串中相應位置的字符來進行替換

maketrans類似於一種規則,經常與translate結合,以完成一些普通函數無法完成的字符串替換

[html] view plain copy
  1. >>> from string import maketrans
  2. >>> table = maketrans(‘cs‘, ‘kz‘)
  3. >>> len(table)
  4. 256
  5. >>> table[97:123]
  6. ‘abkdefghijklmnopqrztuvwxyz‘
  7. >>> maketrans(‘‘,‘‘)[97:123]
  8. ‘abcdefghijklmnopqrstuvwxyz‘

translate函數:功能同上,但是只能處理單個字符,有2個參數,第一個是替換,第二個是刪除

例:table承繼maketrans中的table

[html] view plain copy
  1. >>> ‘this is an incredible test‘.translate(table)
  2. ‘thiz iz an inkredible tezt‘
  3. >>> ‘this is an incredible test‘.translate(table, ‘ ‘)
  4. ‘thizizaninkredibletezt‘

5、strip函數

去除兩側(不包括內部)空格的字符串,原序列不變

[html] view plain copy
  1. >>> word = ‘ this is test ‘
  2. >>> word.strip()
  3. ‘this is test‘
  4. >>> word
  5. ‘ this is test ‘
可在strip()加入參數,以去除想要去掉的指定字符

[html] view plain copy
  1. >>> ‘*** SPAM * for * everyone!!! ***‘.strip(‘*‘)
  2. ‘ SPAM * for * everyone!!! ‘
  3. >>> ‘*** SPAM * for * everyone!!! ***‘.strip(‘* ‘)
  4. ‘SPAM * for * everyone!!!‘
  5. >>> ‘*** SPAM * for * everyone!!! ***‘.strip(‘* !‘)
  6. ‘SPAM * for * everyone‘

Python:字符串函數