Home > Backend Development > Python Tutorial > How to output Chinese characters in python

How to output Chinese characters in python

爱喝马黛茶的安东尼
Release: 2019-08-01 14:29:32
Original
14053 people have browsed it

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"))
Copy after login

When saving, it still prompts:

How to output Chinese characters in python

shell output:

ѧϰ
ѧϰ
Copy after login

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"))
Copy after login

shell output:

helloworld
学习
学习
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template