print japanese kana in python

To print Japanese kana in Python, the easiest way is to use Unicode strings. Unicode is a universal system that assigns a unique number to every character in every writing system, including Japanese. Python 3.x natively supports Unicode strings and literals, so a string of Unicode kana characters can be printed as-is:

main.py
# -*- coding: utf-8 -*-

kana_str = "あいうえお"  # a string of Unicode kana characters
print(kana_str)  # prints "あいうえお"
117 chars
5 lines

Note the # -*- coding: utf-8 -*- line at the top. This tells Python to interpret the Python source code as UTF-8, which is the most common encoding for Japanese text.

If you are using Python 2.x, you will need to put a "u" in front of the string to indicate that it is a Unicode string:

main.py
# -*- coding: utf-8 -*-

kana_str = u"あいうえお"  # a Unicode kana string
print(kana_str)  # prints "あいうえお"
104 chars
5 lines

If you need to work with non-ASCII characters extensively, it is recommended to use a Unicode-aware text editor like Sublime Text or PyCharm.

gistlibby LogSnag