Title page doesn't work on special characters - Python

I am trying to capitalize on a lot of strings and some of them start with utf-8 characters. The problem is they don't capitalize!

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

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

      

It is the same with vowels containing `` ^ ¨ and symbols ð, þ, etc. What should I do to solve this problem?

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

+3


source to share


2 answers


You omit u

. the string must be defined as unicode for python!

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

      



As in python 3

, you don't need the default unicode strings u

.

>>> "æthelred".capitalize()
'Æthelred'

      

+5


source


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

from __future__ import unicode_literals

      



This will force Python 3 to behave like strings, making them unicode the default.

+2


source







All Articles