What is the equivalence in Python 3 of letters in Python 2?
Asked Answered
R

1

30

In Python 2 you get

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

But in Python 3, you get

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

It's not defined, whereas digits and whitespace are.

What is the equivalence of letters from the string module in Python 3?

Rangel answered 5/3, 2012 at 3:51 Comment(0)
S
51

Try using: string.ascii_letters instead of just letters, here.

More information here: http://docs.python.org/release/3.1.3/library/string.html#string-constants


Update:

As @wim noted in the previously posted comment, this suggestion to use string.ascii_letters in Python 3 is not equivalent to the letters in Python 2. Like wim noted, string.ascii_letters is not locale-dependent while letters is locale-dependent.

I hope this suggestion can still be helpful, though, but wanted to include the feedback from @wim and the docs.

Suzysuzzy answered 5/3, 2012 at 3:54 Comment(4)
@CosmicRabbitMediaInc: Note that that only will work if you only even encounter English text.Columbary
For me I had to remove the string. part. simply using ascii_letters works in my case on python 3.6Thunderbolt
–1 This is not equivalent. The specific value of string.letters is locale-dependent, and string.ascii_letters is not.Mandle
AttributeError: module 'string' has no attribute 'letters'Turpentine

© 2022 - 2024 — McMap. All rights reserved.