Python2中的print用法
在Python2 中 print 是一種輸出語句
strHello = 'Hello Python'
print strHello
# Hello Python
1.格式化輸出整數(shù)
strHello = "the length of (%s) is %d" %('Hello Wordld', len('Hello World'))
print strHello
# the length of (Hello Wordld) is 11
2.格式化輸出16進(jìn)制整數(shù)
# 格式 描述
# %% 百分號(hào)標(biāo)記
# %c 字符及其ASCII碼
# %s 字符串
# %d 有符號(hào)整數(shù)(十進(jìn)制)
# %u 無符號(hào)整數(shù)(十進(jìn)制)
# %o 無符號(hào)整數(shù)(八進(jìn)制)
# %x 無符號(hào)整數(shù)(十六進(jìn)制)
# %X 無符號(hào)整數(shù)(十六進(jìn)制大寫字符)
# %e 浮點(diǎn)數(shù)字(科學(xué)計(jì)數(shù)法)
# %E 浮點(diǎn)數(shù)字(科學(xué)計(jì)數(shù)法区岗,用E代替e)
# %f 浮點(diǎn)數(shù)字(用小數(shù)點(diǎn)符號(hào))
# %g 浮點(diǎn)數(shù)字(根據(jù)值的大小采用%e或%f)
# %G 浮點(diǎn)數(shù)字(類似于%g)
# %p 指針(用十六進(jìn)制打印值的內(nèi)存地址)
# %n 存儲(chǔ)輸出字符的數(shù)量放進(jìn)參數(shù)列表的下一個(gè)變量中
nHex = 0x20
print 'nHex = %x, nDec = %d, nOct = %o' %(nHex, nHex, nHex)
# nHex = 20, nDec = 32, nOct = 40
輸出二進(jìn)制的話,可以使用python函數(shù)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.格式化輸出浮點(diǎn)數(shù)(float)
- %字符:標(biāo)記轉(zhuǎn)換說明符的開始
- 最小字段寬度:轉(zhuǎn)換后的字符串至少應(yīng)該具有該值指定的寬度窜醉。如果是*荤胁,則寬度會(huì)從值元組中讀出
- 轉(zhuǎn)換標(biāo)志:-表示左對齊瞧预;+表示在轉(zhuǎn)換值之前要加上正負(fù)號(hào);''(空白字符)表示正數(shù)之前保留空格仅政;0表示轉(zhuǎn)換值若位數(shù)不夠則用0填充
- 點(diǎn)(.)后跟精度值:如果轉(zhuǎn)換的是實(shí)數(shù)垢油,精度值就表示出現(xiàn)在小數(shù)點(diǎn)后的位數(shù)。如果轉(zhuǎn)換的是字符串圆丹,那么該數(shù)字就表示最大字段寬度滩愁。如果是*,那么精度將從元組中讀出
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 自動(dòng)換行
# print會(huì)在行末加上回車辫封,如果不需要硝枉,只需在print語句結(jié)尾添加一個(gè)逗號(hào)','
for i in range(0,5):
print i,
# 0 1 2 3 4
或者直接使用下面的函數(shù)進(jìn)行輸出:
import sys
sys.stdout.write("輸出的字符串")
8.萬能的 %r
它可以將后面給的參數(shù)原樣打印出來,帶有類型信息
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 是一個(gè)函數(shù),通過格式化函數(shù)format()來控制輸出格式
1. 通過位置標(biāo)號(hào)
# {0}表示第一個(gè)元素倦微, {1}表示第二個(gè)元素妻味, {2}表示第三個(gè)元素,以此類推欣福。责球。。
a = 'Ace'
b = 'hello'
print("{1}, my name is {0}".format(a, b))
# hello, my name is Ace
2. 通過關(guān)鍵詞參數(shù)
name = "Ace"
age = 26
print("{myname}'s age is {myage}".format(myname=name, myage=age))
# Ace's age is 26
3. 通過屬性和下標(biāo)
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
字典字符串不需要加引號(hào)
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}
在序號(hào)后面加上格式符就可以了拓劝,不用加%
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!!!*