在 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
它没有定义,而 digits
和 whitespace
是。
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-dependent 而 letters
is locale-dependent .
我希望这个建议仍然有用,但希望包含来自 @wim 和文档的反馈。
关于python - Python 2 中的字母在 Python 3 中的等价性是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9561432/