3

I've been trying to capitalize loads of strings, and some of them start with utf-8 characters. Problem is, they don't capitalize!

mystring = 'lucas'
mystring.capitalize() # returns 'Lucas'

mytring = 'æthelred'
mystring.capitalize() # returns 'æthelred'

Same with vowels containing `´^¨ and the characters ð, þ, e.t.c. What do I do to solve this?

I actually don't have access to the string, I get them somewhere else, in a text file...

2 Answers2

5

You omit u . the string needs to be defined as unicode for python !

>>> mytring = u"æthelred"
>>> print mytring.capitalize()
Æthelred

As in python 3 strings are unicode by default you dont need u .

>>> "æthelred".capitalize()
'Æthelred'
Mazdak
  • 105,000
  • 18
  • 159
  • 188
2

If you are using Python 2 this will also work. At the top of your file put:

from __future__ import unicode_literals

This will force Python 3 like behavior for strings, making them unicode by default.

Matt Smith
  • 425
  • 4
  • 9
  • this doesn't seem to work, there are several stuff it can't decode this way. I'm also declaring utf-8 at the beggining of the file, it shouldn't be a problem. – Leandro Lyra Dec 31 '14 at 19:11