1. 程式人生 > >Python中的string模組的學習

Python中的string模組的學習

程式碼為主,相信有python基礎的都能看懂:

?
  1. >>> import string  
  2. >>> string.ascii_letters  
  3. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  4. >>> string.ascii_lowercase  
  5. 'abcdefghijklmnopqrstuvwxyz'
  6. >>> string.ascii_uppercase  
  7. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  8. >>> string.digits  
  9. '0123456789'
  10. >>> string.hexdigits  
  11. '0123456789abcdefABCDEF'
  12. >>> string.letters  
  13. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  14. >>> string.lowercase  
  15. 'abcdefghijklmnopqrstuvwxyz'
  16. >>> string.uppercase  
  17. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  18. >>> string.octdigits  
  19. '01234567'
  20. >>> string.punctuation  
  21. '!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~'
  22. >>> string.printable  
  23. '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~ \t\n\r\x0b\x0c'
  24. >>> string.whitespace  
  25. '\t\n\x0b\x0c\r  

  1. >>> '{0}, {1}, {2}'.format('a''b''c')  
  2. 'a, b, c'
  3. >>> '{}, {}, {}'.format('a''b''c')  # 2.7+ only
  4. 'a, b, c'
  5. >>> '{2}, {1}, {0}'.format('a''b''c')  
  6. 'c, b, a'
  7. >>> '{2}, {1}, {0}'.format(*'abc')      # unpacking argument sequence
  8. 'c, b, a'
  9. >>> '{0}{1}{0}'.format('abra''cad')   # arguments' indices can be repeated
  10. 'abracadabra'
  11. >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')  
  12. 'Coordinates: 37.24N, -115.81W'
  13. >>> coord = {'latitude''37.24N''longitude''-115.81W'}  
  14. >>> 'Coordinates: {latitude}, {longitude}'.format(**coord)  
  15. 'Coordinates: 37.24N, -115.81W'
  16. >>> c = 3-5j
  17. >>> ('The complex number {0} is formed from the real part {0.real} '
  18. ...  'and the imaginary part {0.imag}.').format(c)  
  19. 'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'
  20. >>> class Point(object):  
  21. ...     def __init__(self, x, y):  
  22. ...         self.x, self.y = x, y  
  23. ...     def __str__(self):  
  24. ...         return'Point({self.x}, {self.y})'.format(self=self)  
  25. ...  
  26. >>> str(Point(42))  
  27. 'Point(42)  
  28. >>> coord = (35)  
  29. >>> 'X: {0[0]};  Y: {0[1]}'.format(coord)  
  30. 'X: 3;  Y: 5'
  31. >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')  
  32. 相關推薦

    no