python - Python 2 中的字母在 Python 3 中的等价性是什么?

标签 python python-3.x

在 Python 2 中你会得到

>>> from string import *
>>> letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

但在 Python 3 中,您会得到

>>> from string import *
>>> letters
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'letters' is not defined

它没有定义,而 digitswhitespace 是。

Python 3 中 string 模块中的 letters 的等价物是什么?

最佳答案

在这里尝试使用:string.ascii_letters 而不仅仅是 letters

更多信息在这里:http://docs.python.org/release/3.1.3/library/string.html#string-constants


更新:

正如@wim 在 previously posted comment 中指出的那样,在 Python 3 中使用 string.ascii_letters 的建议并不等同于 Python 2 中的 letters。就像 wim 指出的那样,string.ascii_letters is not locale-dependentletters is locale-dependent .

我希望这个建议仍然有用,但希望包含来自 @wim 和文档的反馈。

关于python - Python 2 中的字母在 Python 3 中的等价性是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9561432/

相关文章:

python - Cassandra写入超时写入

python - Python 中的均方误差

python - 如何让 pyautogui click 在 mac 上运行?

python - 如果 panda 中的值为 Nan,则合并两行

python - 如何在 Django 中编写复杂的 Mysql 查询

Python Numpy : Operation on each sequential element without for loop?

python - 给定一个用给定基向量构建的函数,逼近矩阵的值

python-3.x - 如何使用 tkinter 在 python 上播放声音

python - 计算 numpy.ndarray 内的度数

python - 自定义 Python 扩展 - 导入错误 : undefined symbol