前言
最近在學習37004.html">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 112.格式化輸出16進制整數
# 格式 描述# %% 百分號標記# %c 字符及其ASCII碼# %s 字符串# %d 有符號整數(十進制)# %u 無符號整數(十進制)# %o 無符號整數(八進制)# %x 無符號整數(十六進制)# %X 無符號整數(十六進制大寫字符)# %e 浮點數字(科學計數法)# %E 浮點數字(科學計數法,用E代替e)# %f 浮點數字(用小數點符號)# %g 浮點數字(根據值的大小采用%e或%f)# %G 浮點數字(類似于%g)# %p 指針(用十六進制打印值的內存地址)# %n 存儲輸出字符的數量放進參數列表的下一個變量中
nHex = 0x20print '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)
import math#defaultprint 'PI = %f' % math.pi# PI = 3.141593# width = 10, precise = 3, align = leftprint 'PI = %10.3fxxx' % math.pi# PI = 3.142xxx# width = 10, precise = 3, align = rightprint 'PI = %-10.3fxxx' % math.pi# PI = 3.142 xxx# 前面填充字符串print 'PI = %06d' % int(math.pi)# PI = 000003
4.格式化輸出字符串(string)
# precise = 3print '%.3s' % ('jcodeer')# jco# precise = 4print '%.*s' % (4,'jcodeer')# jcod# width = 10, precise = 3print 'xx%10.3s' % ('jcodeer')# xx jco5.輸出列表(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 syssys.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 npa = 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 Ace2. 通過關鍵詞參數
name = "Ace"age = 26print("{myname}'s age is {myage}".format(myname=name, myage=age))# Ace's age is 263. 通過屬性和下標
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 264. 格式化限定符
{0:0.3f} {1:3d} 在序號后面加上格式符就可以了,不用加%
5.填充與對齊
^,<,>分別代表居住,左對齊,右對齊,后面帶寬度
a = 123.456789haha = '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!!!*總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。
新聞熱點
疑難解答