diferent answers with isalpha()

Jyotirmoy Bhattacharya jmoy.matecon at gmail.com
Fri Jul 13 07:07:14 CEST 2007

On Jul 13, 5:05 am, n... at salgado.ws wrote:
> In Idle when I do print 'á'.isalpha() I get True. When I make and
> execute a script file with the same code I get False.
> Why do I have diferent answers ?

Non-ASCII characters in ordinary (8-bit) strings have all kinds of
strangeness. First, the answer of isalpha() and friends depends on the
current locale. By default, Python uses the "C" locale where the
alphabetic characters are a-zA-z only. To set the locale to whatever
is the OS setting for the current user, put this near the beginning of
your script:

import locale

Apparently IDLE does this for you. Hence the discrepancy you noted.

Second, there is the matter of encoding. String literals like the one
you used in your example are stored in whatever encoding your text
editor chose to store your program in. If it doesn't match the
encoding using by the current locale, once again the program fails.

As I see it, the only way to properly handle characters outside the
ASCII set is to use Unicode strings.

More information about the Python-list mailing list