Home > Article > Backend Development > How to output Chinese characters in python
Watch the Python concise tutorial and learn to use print to print strings. I tried printing Chinese, but it didn’t work.
Editing environment: IDLE
I searched the Internet for solutions and found various explanations. I tried two:
print u"学习" print (unicode("学习", encoding="utf-8"))
When saving, it still prompts:
shell output:
ѧϰ ѧϰ
It means that the second type still has encoding but it is still wrong.
Related recommendations: "Python Video Tutorial"
Finally, just add the first line. Although I still don’t understand what the first line is, it’s not that# Is it 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 compilation error reported in the past. Python is a scripting language and does not compile. , so it will prompt when saving.
Feedback when saving: python # -*- coding: cp936 -*-means to specify the character set used when saving the code.
About CP936: It is GBK. When IBM invented the Code Page, it placed GBK on page 936, so it is called CP936.
So the encoding statement in the first line can also be specified with #-*- coding: UTF-8 -*-.
The above is the detailed content of How to output Chinese characters in python. For more information, please follow other related articles on the PHP Chinese website!