Python2和Python3中print的⽤法⽰例总结
前⾔
最近在学习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]])
python格式化输出formatprint 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*
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作具有⼀定的参考学习价值,如果有疑问⼤家可以留⾔交流,谢谢⼤家对的⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论