python学习

文章介绍了Python中Print函数的使用,包括如何处理中文乱码问题,如UnicodeDecodeError的解决方法,以及在Python2和Python3中print函数的不同,特别是在Python3中通过设置end参数实现不换行的效果。此外,还提到了使用json库处理含有中文的列表或字典,以及Python2到Python3的兼容性问题。
摘要由CSDN通过智能技术生成

学习网站一:
http://c.biancheng.net/view/2187.html
Python中Print()函数的用法___实例详解(全,例多)
https://blog.csdn.net/weixin_69553582/article/details/125403845
Python 文档目录
https://docs.python.org/zh-cn/3/contents.html
python Format()函数的用法
https://blog.csdn.net/weixin_69553582/article/details/129830874
在这里插入图片描述
内置函数:
在这里插入图片描述

内置函数的具体功能和用法,可通过访问 https://docs.python.org/zh-cn/3/library/functions.html 进行查看

=============================================================================
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe8 in position 39: ordinal not in range(128)
解决方法:

在Python程序最前面加上以下语句:

import sys
reload(sys)
sys.setdefaultencoding(‘utf-8’)

将默认编码设置为utf-8


python处理列表或字典包含中文乱码问题

解码之前

dict = {‘name’: ‘张三’}
print dict
{‘name’: ‘xe5xbcxa0xe4xb8x89’}

使用json解码后

import json
dict = {‘name’: ‘张三’}
print json.dumps(dict, encoding=“UTF-8”, ensure_ascii=False)
{‘name’: ‘张三’}

例子2:在这里插入图片描述

===============================================================================

Python2 与 Python3 print 不换行

Python 3.x
在 Python 3.x 中,我们可以在 print() 函数中添加 end=“” 参数,这样就可以实现不换行效果。

在 Python3 中, print 函数的参数 end 默认值为 “\n”,即end=“\n”,表示换行,给 end 赋值为空, 即end=“”,就不会换行了,例如:

在这里插入图片描述

end=“” 可以设置一些特色符号或字符串:

在这里插入图片描述

Python2.x 实例
在这里插入图片描述

如果有变量,我们可以在逗号 , 后面直接添加变量:

在这里插入图片描述

注意:这种输出方法,在 , 末尾会有一个空格输出,类似于使用了 Python 3 的 end=" "。

Python2.x 与 Python3.x 兼容模式
如果 Python2.x 版本想使用 Python3.x 的 print 函数,可以导入 future 包,该包禁用 Python2.x 的 print 语句,采用 Python3.x 的 print 函数。

以下代码在 Python2.x 与 Python3.x 都能正确执行:

在这里插入图片描述

注:Python3.x 与 Python2.x 的许多兼容性设计的功能可以通过 future 这个包来导入。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值