如何使用 Python 获取所有 ASCII 字符的列表?
我正在寻找类似以下内容的内容:
import ascii
print(ascii.charlist())
它将返回类似 ["A", "B", "C", "D" ... ]
的内容。
I'm looking for something like the following:
import ascii
print(ascii.charlist())
Which would return something like ["A", "B", "C", "D" ... ]
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
string
模块中的常量可能是你想要什么。所有 ASCII 大写字母:
所有 可打印 ASCII 字符:
对于每个单个字符ASCII 标准中定义的字符,请使用
chr
:The constants in the
string
module may be what you want.All ASCII capital letters:
All printable ASCII characters:
For every single character defined in the ASCII standard, use
chr
:这里是:
Here it is:
ASCII 定义了 128 个字符,其字节值范围为 0 到 127(含)。因此,要获取所有 ASCII 字符的字符串,您只需执行“
仅其中 100 个字符被视为可打印”。可打印的 ASCII 字符可以通过以下方式访问
ASCII defines 128 characters whose byte values range from 0 to 127 inclusive. So to get a string of all the ASCII characters, you could just do
Only 100 of those are considered printable. The printable ASCII characters can be accessed via
由于 ASCII 可打印字符是一个非常小的列表(值在 32 到 126 之间的字节),因此在需要时很容易生成:
Since ASCII printable characters are a pretty small list (bytes with values between 32 and 126 inclusive), it's easy enough to generate when you need:
您可以在没有模块的情况下执行此操作:
输入
characters
,它应该打印["a","b","c", ... ,"x","y"," z"]
。对于大写使用:任何范围(包括范围步骤的使用)都可以用于此目的,因为它使用 Unicode。因此,增加
range()
以向列表中添加更多字符。map()
在range()
的每次迭代中调用chr()
。You can do this without a module:
Type
characters
and it should print["a","b","c", ... ,"x","y","z"]
. For uppercase use:Any range (including the use of range steps) can be used for this, because it makes use of Unicode. Therefore, increase the
range()
to add more characters to the list.map()
callschr()
every iteration of therange()
.不,没有,但你可以轻松制作一个:
No, there isn't, but you can easily make one: