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

How to print Chinese characters in python

(*-*)浩
(*-*)浩Original
2019-07-01 13:48:016480browse

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.

How to print 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. (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!

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