Home > Article > Backend Development > How to print Chinese characters in python
Today I will share with you a method of using print to output Chinese in Python. It has a good reference value and I hope it will be helpful to everyone.
Watch the Python concise tutorial and learn to use print to print strings. I tried printing Chinese, but it didn’t work. (Recommended learning: Python video tutorial)
Editing environment: IDLE
I searched the Internet for solutions, various opinions, and tried two:
print u"学习" print (unicode("学习", encoding="utf-8"))
An error will still be prompted when saving.
Shell output:
ѧϰ ѧϰ
It means that the first one is still encoded but it is still wrong.
Finally, just add the first line. Although I still don’t understand what the first line is, isn’t it that # is a comment? . .
# encoding: utf-8 print 'helloworld' print u"学习" print (unicode("学习", encoding="utf-8"))
shell output:
helloworld 学习 学习
Finally: I found that when writing code, I still need to pay attention to the editor’s feedback. I only think about the kind of C compiler errors in the past. Python is a scripting language. Compilation is not performed, so you will be prompted when saving.
Feedback when saving: python # -*- coding: cp936 -*- means specifying the character set used when saving the code.
So the encoding statement in the first line can also be specified with #-*- coding: UTF-8 -*-.
For more Python-related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to print Chinese characters in python. For more information, please follow other related articles on the PHP Chinese website!