How to capitalize words containing non-ASCII characters in Python? Is there a way to tune string
's capitalize()
method to do that?
Asked
Active
Viewed 2,496 times
5

RichieHindle
- 272,464
- 47
- 358
- 399

Alex
- 43,191
- 44
- 96
- 127
2 Answers
10
Use Unicode strings:
# coding: cp1252
print u"é".capitalize()
# Prints É
If all you have is an 8-bit string, decode it into Unicode first:
# coding: cp1252
print "é".decode('cp1252').capitalize()
# Prints É
If you then need it as an 8-bit string again, encode it:
# coding: cp1252
print "é".decode('cp1252').capitalize().encode('cp1252')
# Prints É (assuming your terminal is happy to receive cp1252)

RichieHindle
- 272,464
- 47
- 358
- 399
-
http://unicode.org/Public/UNIDATA/SpecialCasing.txt says that it is not that simple even when locale is not under consideration – jfs Jun 17 '09 at 12:45