Python中的string模組的學習
阿新 • • 發佈:2019-01-18
程式碼為主,相信有python基礎的都能看懂:
?- >>> import string
- >>> string.ascii_letters
- 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
- >>> string.ascii_lowercase
- 'abcdefghijklmnopqrstuvwxyz'
- >>> string.ascii_uppercase
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- >>> string.digits
- '0123456789'
- >>> string.hexdigits
- '0123456789abcdefABCDEF'
- >>> string.letters
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
- >>> string.lowercase
- 'abcdefghijklmnopqrstuvwxyz'
- >>> string.uppercase
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- >>> string.octdigits
- '01234567'
- >>> string.punctuation
- '!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~'
- >>> string.printable
- '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~ \t\n\r\x0b\x0c'
- >>> string.whitespace
- '\t\n\x0b\x0c\r
- >>> '{0}, {1}, {2}'.format('a', 'b', 'c')
- 'a, b, c'
- >>> '{}, {}, {}'.format('a', 'b', 'c') # 2.7+ only
- 'a, b, c'
- >>> '{2}, {1}, {0}'.format('a', 'b', 'c')
- 'c, b, a'
- >>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence
- 'c, b, a'
- >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated
- 'abracadabra'
- >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
- 'Coordinates: 37.24N, -115.81W'
- >>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
- >>> 'Coordinates: {latitude}, {longitude}'.format(**coord)
- 'Coordinates: 37.24N, -115.81W'
- >>> c = 3-5j
- >>> ('The complex number {0} is formed from the real part {0.real} '
- ... 'and the imaginary part {0.imag}.').format(c)
- 'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'
- >>> class Point(object):
- ... def __init__(self, x, y):
- ... self.x, self.y = x, y
- ... def __str__(self):
- ... return'Point({self.x}, {self.y})'.format(self=self)
- ...
- >>> str(Point(4, 2))
- 'Point(4, 2)
- >>> coord = (3, 5)
- >>> 'X: {0[0]}; Y: {0[1]}'.format(coord)
- 'X: 3; Y: 5'
- >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
-
相關推薦
no