Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalizing non-ASCII words in Python

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

like image 507
Alex Avatar asked Jun 17 '09 11:06

Alex


2 Answers

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)
like image 82
RichieHindle Avatar answered Sep 21 '22 07:09

RichieHindle


capitalize() should Just Work™ for Unicode strings.

like image 35
Hank Gay Avatar answered Sep 20 '22 07:09

Hank Gay