Home  >  Article  >  Backend Development  >  How to output Chinese characters in python

How to output Chinese characters in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-01 14:29:3214004browse

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:

How to output Chinese characters in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn