How to capitalize words containing non-ASCII characters in Python? Is there a way to tune string
's capitalize()
method to do that?
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)
capitalize()
should Just Work™ for Unicode strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With