当前位置:   article > 正文

Python 标准库大全之 string模块_python string库

python string库

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
"""
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
翻译
“”“字符串常量的集合。
公用模块变量:
whitespace   -包含所有ASCII空格的字符串
ascii_lowercase   -包含所有ASCII小写字母的字符串
ascii_uppercase   -包含所有ASCII大写字母的字符串
ascii_letters   -包含所有ASCII字母的字符串
digits   -包含所有ASCII十进制数字的字符串
hexdigits   -包含所有ASCII十六进制数字的字符串
octdigits   -包含所有ASCII八进制数字的字符串
punctuation   -包含所有ASCII标点符号的字符串
printable   -包含所有可打印的ASCII字符的字符串
“”
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

导入string模块

为Python自带标准库

import string
  • 1

字符串常量

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")
  • 1
  • 2
  • 3

打印返回结果
下文所述 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")
  • 1
  • 2
  • 3

打印返回结果

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")
  • 1
  • 2
  • 3

打印返回结果

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")
  • 1
  • 2
  • 3

打印返回结果

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")
  • 1
  • 2
  • 3

打印返回结果

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")
  • 1
  • 2
  • 3

打印返回结果

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")
  • 1
  • 2
  • 3

打印返回结果

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")
  • 1
  • 2
  • 3

打印返回结果

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")
  • 1
  • 2
  • 3

打印返回结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/54583
推荐阅读
相关标签
  

闽ICP备14008679号