python 输出及乱码解决

本文详细介绍了Python中打印语句的用法,包括在Python 2和Python 3中的不同写法,展示了如何正确地输出包含中文的字符串,并解释了编码声明的重要性。

ex1.py文件

# coding: utf-8
print "hello world!"

print "Hello Again"

print "I like typing this."

print "This is fun."

print 'Yay ! Pring.'

print "I'd much rather you 'not'."

print 'I "said" do not touch this.'

print "这是中文输出"

输出语句写法如上

如果不加第一行, 当输出有中文时, 会报出以下错误:

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

以上所述为python2版本, 如果使用python3版本, 输出会有变化, python3将print变成了函数, 输出时, 需要加括号

# coding: utf-8
print("hello world!")

print("Hello Again")

print("I like typing this.")

print("This is fun.")

print('Yay ! Pring.')

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、付费专栏及课程。

余额充值