python 2.7 lowercase

Yebach picture Yebach · Mar 30, 2012 · Viewed 27.6k times · Source

When I use .lower() in Python 2.7, string is not converted to lowercase for letters ŠČŽ. I read data from dictionary.

I tried using str(tt["code"]).lower(), tt["code"].lower().

Any suggestions ?

Answer

CR Drost picture CR Drost · Mar 30, 2012

Use unicode strings:

drostie@signy:~$ python
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "ŠČŽ"
ŠČŽ
>>> print "ŠČŽ".lower()
ŠČŽ
>>> print u"ŠČŽ".lower()
ščž

See that little u? That means that it's created as a unicode object rather than a str object.