Convert unicode cyrillic symbols to string in python

python, python-2.7, string, unicode

Solution

Pick an encoding that can encode Cyrillic symbols, such as UTF-8:

>>> a = u'Тест'

>>> a.encode('utf-8')
'\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82'

The ASCII table doesn't have code points for Cyrillic characters, so you need to specify an encoding explicitly.

But if all you want is just print the string, then what you should care about is the encoding of your terminal and the system font.

Problem

When I try to convert unicode: ``` a = u"Тест" ``` To string: ``` str(a) ``` I got this error: ``` 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128) ``` I need str(a) to give me output: ``` >> str(a) >> 'Тест' ```

Original source

Related problems