python3 (import stri

2023-01-31 02:01:56 import python3 stri

string模块中定义了一些常用的属性(包含所有数字,字母,可打印的所有ascii码等)

print(string.ascii_letters)     # 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
print(string.ascii_lowercase)   # 'abcdefghijklmnopqrstuvwxyz'
print(string.ascii_uppercase)   # 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
print(string.digits)            # '0123456789'
print(string.hexdigits)         # '0123456789abcdefABCDEF'
print(string.octdigits)         # '01234567'
print(string.printable)         # '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU...
print(string.punctuation)       # '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
print(string.whitespace)        # ' \t\n\r\x0b\x0c'

string.capWords(s, sep=None)是多个字符串方法的组合split(),capitalize(),join()

s = 'ahelloaworld'
print(string.capwords(s, 'a'))

等同于

print('a'.join([i.capitalize() for i in s.split('a')]))

结果为:aHelloaWorld

相关文章