Python2和Python3中print的用法示例总结


Posted in Python onOctober 25, 2017

前言

最近在学习python,对于python的print一直很恼火,老是不按照预期输出。在python2中print是一种输出语句,和if语句,while语句一样的东西,在python3中为了填补python2的各种坑,将print变为函数,因此导致python3中print的一些使用和python2很不一样。下面就来给大家详细的总结了关于Python2和Python3中print的用法,话不多说了,来一起看看详细的介绍吧。

一、Python2中的print用法

在Python2 中 print 是一种输出语句

strHello = 'Hello Python'
print strHello
# Hello Python

1.格式化输出整数

strHello = "the length of (%s) is %d" %('Hello Wordld', len('Hello World'))
print strHello
# the length of (Hello Wordld) is 11

2.格式化输出16进制整数

# 格式 描述
# %% 百分号标记
# %c 字符及其ASCII码
# %s 字符串
# %d 有符号整数(十进制)
# %u 无符号整数(十进制)
# %o 无符号整数(八进制)
# %x 无符号整数(十六进制)
# %X 无符号整数(十六进制大写字符)
# %e 浮点数字(科学计数法)
# %E 浮点数字(科学计数法,用E代替e)
# %f 浮点数字(用小数点符号)
# %g 浮点数字(根据值的大小采用%e或%f)
# %G 浮点数字(类似于%g)
# %p 指针(用十六进制打印值的内存地址)
# %n 存储输出字符的数量放进参数列表的下一个变量中
nHex = 0x20
print 'nHex = %x, nDec = %d, nOct = %o' %(nHex, nHex, nHex)
# nHex = 20, nDec = 32, nOct = 40

输出二进制的话,可以使用python函数bin()

# Python 2.7.10 (default, Feb 7 2017, 00:08:15)
# [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
# Type "help", "copyright", "credits" or "license" for more information.
# >>> bin(789)
# '0b1100010101'
# >>>

3.格式化输出浮点数(float)

  • %字符:标记转换说明符的开始
  • 最小字段宽度:转换后的字符串至少应该具有该值指定的宽度。如果是*,则宽度会从值元组中读出
  • 转换标志:-表示左对齐;+表示在转换值之前要加上正负号;''(空白字符)表示正数之前保留空格;0表示转换值若位数不够则用0填充
  • 点(.)后跟精度值:如果转换的是实数,精度值就表示出现在小数点后的位数。如果转换的是字符串,那么该数字就表示最大字段宽度。如果是*,那么精度将从元组中读出
import math
#default
print 'PI = %f' % math.pi
# PI = 3.141593

# width = 10, precise = 3, align = left
print 'PI = %10.3fxxx' % math.pi
# PI =  3.142xxx

# width = 10, precise = 3, align = right
print 'PI = %-10.3fxxx' % math.pi
# PI = 3.142  xxx

# 前面填充字符串
print 'PI = %06d' % int(math.pi)
# PI = 000003

4.格式化输出字符串(string)

# precise = 3
print '%.3s' % ('jcodeer')
# jco

# precise = 4
print '%.*s' % (4,'jcodeer')
# jcod

# width = 10, precise = 3
print 'xx%10.3s' % ('jcodeer')
# xx  jco

5.输出列表(list)

l = [1, 2, 3, 'jcodeer']
print l
# [1, 2, 3, 'jcodeer']

6.输出字典(dictionary)

d = {1: 'A',2: 'B',3: 'C',4: 'D'}
print d
# {1: 'A', 2: 'B', 3: 'C', 4: 'D'}

7.python print 自动换行

# print会在行末加上回车,如果不需要,只需在print语句结尾添加一个逗号','
for i in range(0,5):
 print i,
# 0 1 2 3 4

或者直接使用下面的函数进行输出:

import sys
sys.stdout.write("输出的字符串")

8.万能的 %r

它可以将后面给的参数原样打印出来,带有类型信息

formatter = '%r %r %r %r'
 
print formatter % (1, 2, 3, 4)
print formatter % ('one', 'two', 'three', 'four')
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
 "But it didn't sing.",
 "So I said goodnight."
)
# 1 2 3 4
# 'one' 'two' 'three' 'four'
# True False False True
# '%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
# 'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'

9.矩阵输出

import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
print a
# [[1 2]
# [3 4]]
 
print b
# [[5 6]
# [7 8]]
 
print a, b
# [[1 2]
# [3 4]] [[5 6]
# [7 8]]

二、Python3中的print用法

在Python3 中print 是一个函数,通过格式化函数format()来控制输出格式

1. 通过位置标号

# {0}表示第一个元素, {1}表示第二个元素, {2}表示第三个元素,以此类推。。。
 
a = 'Ace'
b = 'hello'
print("{1}, my name is {0}".format(a, b))
# hello, my name is Ace

2. 通过关键词参数

name = "Ace"
age = 26
print("{myname}'s age is {myage}".format(myname=name, myage=age))
# Ace's age is 26

3. 通过属性和下标

person = ["Ace", 26]
print("{0[0]}'s age is {0[1]}".format(person))
# Ace's age is 26
  
print("{people[0]}'s age is {people[1]}".format(people=person))
# Ace's age is 26

字典字符串不需要加引号

person = {'Ace': 26}
print("{myname}'s age is {people[Ace]}".format(myname=name,people=person))
# Ace's age is 26

4. 格式化限定符

{0:0.3f} {1:3d} 在序号后面加上格式符就可以了,不用加%

5.填充与对齐

^,<,>分别代表居住,左对齐,右对齐,后面带宽度

a = 123.456789
haha = 'haha!!!'
print("{0:0.3f}, *{1:<14}*".format(a, haha))
print("{0:0.3f}, *{1:>14}*".format(a, haha))
print("{0:0.3f}, *{1:^14}*".format(a, haha))
print("{0:0.3f}, *{1:}*".format(a, haha))
  
# 123.457, *haha!!!    *
# 123.457, *    haha!!!*
# 123.457, *  haha!!!  *
# 123.457, *haha!!!*

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
零基础写python爬虫之神器正则表达式
Nov 06 Python
Python的Tornado框架异步编程入门实例
Apr 24 Python
python判断windows系统是32位还是64位的方法
May 11 Python
Python实现控制台输入密码的方法
May 29 Python
Mac 上切换Python多版本
Jun 17 Python
TensorFlow模型保存/载入的两种方法
Mar 08 Python
python控制nao机器人身体动作实例详解
Apr 29 Python
python3+PyQt5 实现Rich文本的行编辑方法
Jun 17 Python
基于pytorch的lstm参数使用详解
Jan 14 Python
详解python 降级到3.6终极解决方案
Feb 06 Python
Python装饰器用法与知识点小结
Mar 09 Python
Python实现Keras搭建神经网络训练分类模型教程
Jun 12 Python
Python_LDA实现方法详解
Oct 25 #Python
python+mongodb数据抓取详细介绍
Oct 25 #Python
python装饰器实例大详解
Oct 25 #Python
Python3 模块、包调用&amp;路径详解
Oct 25 #Python
Python探索之创建二叉树
Oct 25 #Python
Python探索之修改Python搜索路径
Oct 25 #Python
python中 logging的使用详解
Oct 25 #Python
You might like
利用文件属性结合Session实现在线人数统计
2006/10/09 PHP
完美解决dedecms中的[html][/html]和[code][/code]问题
2007/03/20 PHP
CentOS 6.2使用yum安装LAMP以及phpMyadmin详解
2013/06/17 PHP
PHP模板引擎Smarty中变量的使用方法示例
2016/04/11 PHP
PHP实现支付宝即时到账功能
2016/12/21 PHP
在 PHP 和 Laravel 中使用 Traits的方法
2019/11/13 PHP
fireworks菜单生成器mm_menu.js在 IE 7.0 显示问题的解决方法
2009/10/20 Javascript
js之ActiveX控件使用说明 new ActiveXObject()
2014/03/03 Javascript
jquery实现图片预加载
2015/12/25 Javascript
jQuery+Ajax实现无刷新操作
2016/01/04 Javascript
关于安卓手机微信浏览器中使用XMLHttpRequest 2上传图片显示字节数为0的解决办法
2016/05/17 Javascript
微信小程序实现运动步数排行功能(可删除)
2018/07/05 Javascript
详解服务端预渲染之Nuxt(介绍篇)
2019/04/07 Javascript
Vuex模块化应用实践示例
2020/02/03 Javascript
JS+css3实现幻灯片轮播图
2020/08/14 Javascript
vue+Element-ui实现分页效果
2020/11/15 Javascript
[01:05:29]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Aster BO3 第二场 1月24日
2021/03/11 DOTA
python获取局域网占带宽最大3个ip的方法
2015/07/09 Python
python去掉空白行的多种实现代码
2018/03/19 Python
python数字图像处理实现直方图与均衡化
2018/05/04 Python
Django+Ajax+jQuery实现网页动态更新的实例
2018/05/28 Python
Python函数参数操作详解
2018/08/03 Python
Python使用matplotlib绘制三维图形示例
2018/08/25 Python
python获取txt文件词向量过程详解
2019/07/05 Python
如何基于python3和Vue实现AES数据加密
2020/03/27 Python
通过实例了解python__slots__使用方法
2020/09/14 Python
HTML5 canvas基本绘图之绘制五角星
2016/06/27 HTML / CSS
瑞士香水购物网站:Parfumcity.ch
2017/01/14 全球购物
澳大利亚在线生活方式商店:Mytopia
2018/07/08 全球购物
什么是规则表达式
2012/05/03 面试题
KTV的创业计划书范文
2014/02/02 职场文书
阳光体育活动方案
2014/02/16 职场文书
《最佳路径》教学反思
2014/04/13 职场文书
2015年评职称个人工作总结
2015/10/15 职场文书
自定义函数实现单词排序并运用于PostgreSQL(实现代码)
2021/04/22 PostgreSQL
【海涛七七解说】DCG第二周:DK VS 天禄
2022/04/01 DOTA