实战 - Python基础 之 中文输出

如果想要在python程序中打印中文,默认情况下,你只会收到错误。在ex1.py中加入打印中文的代码,改成如下:

print "Hello Python!"
print "Hello Again."
print "I like typing this."
print "This is fun."
print "Yay!Printing."
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'
print "哈喽!"

再运行,就会发现报出如下错误。

File "ex1.py", line 8
SyntaxError: Non-ASCII character '\xe5' in file ex1.py on line 8, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

那么实在需要打印中文,只需要在.py文件的首行加入编码格式说明(#– coding:utf-8 –)就可以。代码如下:

#-- coding:utf-8 --
print "Hello Python!"
print "Hello Again."
print "I like typing this."
print "This is fun."
print "Yay!Printing."
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'
print "哈喽!"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值