Python中的string模块 —常见的字符串操作
文章目录
模块源码说明
原文
"""A collection of string constants.
Public module variables:
whitespace -- a string containing all ASCII whitespace
ascii_lowercase -- a string containing all ASCII lowercase letters
ascii_uppercase -- a string containing all ASCII uppercase letters
ascii_letters -- a string containing all ASCII letters
digits -- a string containing all ASCII decimal digits
hexdigits -- a string containing all ASCII hexadecimal digits
octdigits -- a string containing all ASCII octal digits
punctuation -- a string containing all ASCII punctuation characters
printable -- a string containing all ASCII characters considered printable
"""
翻译
“”“字符串常量的集合。
公用模块变量:
whitespace -包含所有ASCII空格的字符串
ascii_lowercase -包含所有ASCII小写字母的字符串
ascii_uppercase -包含所有ASCII大写字母的字符串
ascii_letters -包含所有ASCII字母的字符串
digits -包含所有ASCII十进制数字的字符串
hexdigits -包含所有ASCII十六进制数字的字符串
octdigits -包含所有ASCII八进制数字的字符串
punctuation -包含所有ASCII标点符号的字符串
printable -包含所有可打印的ASCII字符的字符串
“”
导入string模块
为Python自带标准库
import string
字符串常量
ascii_letters --所有字母的字符串
string.ascii_letters
包含所有的 ASCII码 字母的字符串
返回类型为字符串
str_dic = string.ascii_letters
print(f"值:\033[1;31m{str_dic}\033[0m")
print(f"数据类型:\033[1;32m{type(str_dic)}\033[0m")
下文所述 ascii_lowercase 和 ascii_uppercase 常量的拼连
ascii_lowercase --小写字母的字符串
string.ascii_lowercase
包含所有 ASCII码 小写字母的字符串
返回类型为字符串
str_dic = string.ascii_lowercase
print(f"值:\033[1;31m{str_dic}\033[0m")
print(f"数据类型:\033[1;32m{type(str_dic)}\033[0m")
ascii_uppercase --大写字母的字符串
string.ascii_uppercase
包含所有 ASCII码 大写字母的字符串
返回类型为字符串
str_dic = string.ascii_uppercase
print(f"值:\033[1;31m{str_dic}\033[0m")
print(f"数据类型:\033[1;32m{type(str_dic)}\033[0m")
digits --十进制数字的字符串
string.digits
包含所有 ASCII码 十进制数字的字符串
返回类型为字符串
str_dic = string.digits
print(f"值:\033[1;31m{str_dic}\033[0m")
print(f"数据类型:\033[1;32m{type(str_dic)}\033[0m")
hexdigits --十六进制数字的字符串
string.hexdigits
包含所有 ASCII码 十六进制数字的字符串
返回类型为字符串
str_dic = string.hexdigits
print(f"值:\033[1;31m{str_dic}\033[0m")
print(f"数据类型:\033[1;32m{type(str_dic)}\033[0m")
octdigits --八进制数字的字符串
string.octdigits
包含所有 ASCII码 八进制数字的字符串
返回类型为字符串
str_dic = string.octdigits
print(f"值:\033[1;31m{str_dic}\033[0m")
print(f"数据类型:\033[1;32m{type(str_dic)}\033[0m")
punctuation --标点符号的字符串
string.punctuation
包含所有 ASCII码 标点符号的字符串
返回类型为字符串
str_dic = string.punctuation
print(f"值:\033[1;31m{str_dic}\033[0m")
print(f"数据类型:\033[1;32m{type(str_dic)}\033[0m")
printable --所有可打印的ASCII码字符的字符串
string.printable
包含所有可打印的 ASCII码 字符的字符串
返回类型为字符串
str_dic = string.printable
print(f"值:\033[1;31m{str_dic}\033[0m")
print(f"数据类型:\033[1;32m{type(str_dic)}\033[0m")
whitespace – 空格的字符串
string.whitespace
包含所有 ASCII码 空格的字符串
返回类型为字符串
str_dic = string.whitespace
print(f"值:\033[1;31m{str_dic}\033[0m")
print(f"数据类型:\033[1;32m{type(str_dic)}\033[0m")