Python2與Python3中print用法總結(jié)

Python2中的print用法

在Python2 中 print 是一種輸出語(yǔ)句

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    無(wú)符號(hào)整數(shù)(十進(jìn)制)
# %o    無(wú)符號(hào)整數(shù)(八進(jìn)制)
# %x    無(wú)符號(hào)整數(shù)(十六進(jìn)制)
# %X    無(wú)符號(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)換說(shuō)明符的開(kāi)始
  • 最小字段寬度:轉(zhuǎn)換后的字符串至少應(yīng)該具有該值指定的寬度。如果是*,則寬度會(huì)從值元組中讀出
  • 轉(zhuǎn)換標(biāo)志:-表示左對(duì)齊;+表示在轉(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語(yǔ)句結(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.萬(wàn)能的 %r

它可以將后面給的參數(shù)原樣打印出來(lái),帶有類型信息

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ù),通過(guò)格式化函數(shù)format()來(lái)控制輸出格式

1. 通過(guò)位置標(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ò)關(guān)鍵詞參數(shù)

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

3. 通過(guò)屬性和下標(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.填充與對(duì)齊

^,<,>分別代表居住,左對(duì)齊,右對(duì)齊,后面帶寬度

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!!!*
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容