Home  >  Article  >  Backend Development  >  How to input Chinese in python

How to input Chinese in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-22 15:28:0211280browse

I just came into contact with the Python language. When I was practicing, I found that Chinese characters could not appear in the script (Chinese is not supported in Python2. The script will crash and cannot be run.

How to input Chinese in python

For example:

name = raw_input("What's your name?")
print "Hello, "+name+"!"
raw_input("Press enter")

Related recommendations: "Python Video Tutorial"

The simple code above is It can run, but if we change the display to:

name = raw_input("你叫什么名字?")
print "你好, "+name+"!"
raw_input("按回车键退出!")

, it will not run. So if you want to input Chinese into the python script, enter in the first line of the code: #coding= utf-8

looks like this:

#coding=utf-8
name = raw_input("你叫什么名字?")
print "你好, "+name+"!"
raw_input("按回车键退出!")

or enter:

# -*- coding: UTF-8 -*-

The above is the detailed content of How to input Chinese 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