To include Unicode characters in your Python source code, you can use Unicode escape characters in the form \u0123 in your string. In Python 2.x, you also need to prefix the string literal with 'u'.
Here's an example running in the Python 2.x interactive console:
>>> print u'\u0420\u043e\u0441\u0441\u0438\u044f'
Россия
In Python 2, prefixing a string with 'u' declares them as Unicode-type variables, as described in the Python Unicode documentation.
In Python 3, the 'u' prefix is now optional:
>>> print('\u0420\u043e\u0441\u0441\u0438\u044f')
Россия
If running the above commands doesn't display the text correctly for you, perhaps your terminal isn't capable of displaying Unicode characters.
These examples use Unicode escapes (\u...), which allows you to print Unicode characters while keeping your source code as plain ASCII. This can help when working with the same source code on different systems. You can also use Unicode characters directly in your Python source code (e.g. print u'Россия' in Python 2), if you are confident all your systems handle Unicode files properly.
For information about reading Unicode data from a file, see this answer:
Character reading from file in Python
Answer from Matt Ryall on Stack OverflowI am seriously confused. And whenever I think I got it, I see some - in my opinion - inconsistent behavior. Can it be consistently explained or is it more art than science?
When do I have to encode/decode("UTF-8")? What does it do exactly? Whats so special about unicode("abc"), or is it identical to u"abc"?
Why, if I'm using a HTML-encoding of UTF8, a python-script with encoding-UTF-8 and a UTF-8 capable shell and have them all interact, do I have to randomly start adding the above functions until stuff accidentally doesn't break anymore? :)
My problem is that while I can code quite well, I have no formal computer science education and don't tend to think in bytes.
To include Unicode characters in your Python source code, you can use Unicode escape characters in the form \u0123 in your string. In Python 2.x, you also need to prefix the string literal with 'u'.
Here's an example running in the Python 2.x interactive console:
>>> print u'\u0420\u043e\u0441\u0441\u0438\u044f'
Россия
In Python 2, prefixing a string with 'u' declares them as Unicode-type variables, as described in the Python Unicode documentation.
In Python 3, the 'u' prefix is now optional:
>>> print('\u0420\u043e\u0441\u0441\u0438\u044f')
Россия
If running the above commands doesn't display the text correctly for you, perhaps your terminal isn't capable of displaying Unicode characters.
These examples use Unicode escapes (\u...), which allows you to print Unicode characters while keeping your source code as plain ASCII. This can help when working with the same source code on different systems. You can also use Unicode characters directly in your Python source code (e.g. print u'Россия' in Python 2), if you are confident all your systems handle Unicode files properly.
For information about reading Unicode data from a file, see this answer:
Character reading from file in Python
Print a unicode character in Python:
Print a unicode character directly from python interpreter:
el@apollo:~$ python
Python 2.7.3
>>> print u'\u2713'
✓
Unicode character u'\u2713' is a checkmark. The interpreter prints the checkmark on the screen.
Print a unicode character from a python script:
Put this in test.py:
#!/usr/bin/python
print("here is your checkmark: " + u'\u2713');
Run it like this:
el@apollo:~$ python test.py
here is your checkmark: ✓
If it doesn't show a checkmark for you, then the problem could be elsewhere, like the terminal settings or something you are doing with stream redirection.
Store unicode characters in a file:
Save this to file: foo.py:
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
import codecs
import sys
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)
print(u'e with obfuscation: é')
Run it and pipe output to file:
python foo.py > tmp.txt
Open tmp.txt and look inside, you see this:
el@apollo:~$ cat tmp.txt
e with obfuscation: é
Thus you have saved unicode e with a obfuscation mark on it to a file.