views:

332

answers:

2

How to capitalize words containing non-ASCII characters in Python? Is there a way to tune string's capitalize() method to do that?

+7  A: 

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
http://unicode.org/Public/UNIDATA/SpecialCasing.txt says that it is not that simple even when locale is not under consideration
J.F. Sebastian
A: 
Hank Gay