Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalence in Python 3 of letters in Python 2?

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?

like image 380
CosmicRabbitMediaInc Avatar asked Mar 05 '12 03:03

CosmicRabbitMediaInc


People also ask

What is the difference between text encoding in Python 2 and Python 3?

In Python 2, the str type was used for two different kinds of values – text and bytes, whereas in Python 3, these are separate and incompatible types. Text contains human-readable messages, represented as a sequence of Unicode codepoints. Usually, it does not contain unprintable control characters such as \0 .

How do you compare letters in python?

You can use ( > , < , <= , <= , == , != ) to compare two strings. Python compares string lexicographically i.e using ASCII value of the characters.

How do you compare part of a string in Python?

Use the in operator for partial matches, i.e., whether one string contains the other string. x in y returns True if x is contained in y ( x is a substring of y ), and False if it is not. If each character of x is contained in y discretely, False is returned.

Do letters have value in Python?

Each character will have its own integer value, and the value differs for uppercase and lowercase characters.


1 Answers

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.

like image 179
summea Avatar answered Sep 20 '22 15:09

summea