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
"""
翻译
“”“字符串常量的集合。
公用模块变量:
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")

打印返回结果

  • 8
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python标准库中的`string`模块提供了一些用于字符串操作的常量和函数。该模块包括以下内容: 1. `string.ascii_letters`:包含所有 ASCII 字母(大写和小写)的字符串。 2. `string.ascii_lowercase`:包含所有 ASCII 小写字母的字符串。 3. `string.ascii_uppercase`:包含所有 ASCII 大写字母的字符串。 4. `string.digits`:包含所有数字字符的字符串。 5. `string.hexdigits`:包含所有十六进制数字字符的字符串。 6. `string.octdigits`:包含所有八进制数字字符的字符串。 7. `string.printable`:包含可打印字符的字符串,包括 ASCII 字母、数字、标点符号和空格。 8. `string.punctuation`:包含所有 ASCII 标点符号的字符串。 9. `string.capwords(s[, sep])`:将字符串中的单词首字母大写并返回。可选参数`sep`指定单词分隔符,默认为空格。 下面是一个使用`string`模块的示例: ```python import string # 打印所有 ASCII 字母 print(string.ascii_letters) # 打印所有 ASCII 小写字母 print(string.ascii_lowercase) # 打印所有 ASCII 大写字母 print(string.ascii_uppercase) # 打印所有数字字符 print(string.digits) # 打印所有十六进制数字字符 print(string.hexdigits) # 打印所有八进制数字字符 print(string.octdigits) # 打印可打印字符 print(string.printable) # 打印所有 ASCII 标点符号 print(string.punctuation) # 将字符串中的单词首字母大写并返回 print(string.capwords("hello world")) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值