溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python3的print()函數(shù)的用法圖文講解

發(fā)布時(shí)間:2020-10-21 19:20:04 來(lái)源:腳本之家 閱讀:328 作者:njzhw 欄目:開(kāi)發(fā)技術(shù)

Python 3 print 函數(shù) 基礎(chǔ)代碼

1、print語(yǔ)法格式

print()函數(shù)具有豐富的功能,詳細(xì)語(yǔ)法格式如下:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

默認(rèn)情況下,將值打印到流或sys.stdout。
可選關(guān)鍵字參數(shù):
file:類文件對(duì)象(stream); 默認(rèn)為當(dāng)前的sys.stdout。
sep:在值之間插入的字符串,默認(rèn)為空格。
end:在最后一個(gè)值后附加的字符串,默認(rèn)為換行符。
flush:是否強(qiáng)制刷新流。

2、sep可選關(guān)鍵字參數(shù)
sep參數(shù)可以在值之間插入字符串,默認(rèn)值為空格。
例:

print('1','2','3','4',sep = "插入")

輸出結(jié)果:

1插入2插入3插入4

3、file可選關(guān)鍵字參數(shù)

file參數(shù)默認(rèn)值為sys.stdout,代表系統(tǒng)標(biāo)準(zhǔn)輸出,即屏幕。我們可以通過(guò)改變?cè)搮?shù)使print()函數(shù)輸出到特定的文件中。
例:

f = open(r"F:\text.txt","w")    # 打開(kāi)文件,以便寫(xiě)入
print('test',file = f)  # 輸出到文件
f.close()   # 關(guān)閉文件

運(yùn)行后,可以看到test輸出到text.txt文件中。

4、end可選關(guān)鍵字參數(shù)

end參數(shù)默認(rèn)為"\n"(換行符),如果想在print()函數(shù)輸出之后輸出別的字符串,可以重設(shè)end參數(shù)。
例:

print('1','2',end = "最后")

輸出結(jié)果:

1 2最后>>>

運(yùn)行后,我們可以看到,print()函數(shù)輸出之后不會(huì)換行,且在最后一個(gè)值后面附加了“最后”。

5、flush可選關(guān)鍵字參數(shù) 

flush參數(shù)用于控制輸出緩存,一般為了可以獲得較好的性能,保持為False即可。

6、print()打印中比%格式符更方便的一個(gè)打印方式print(f"")

有沒(méi)有小伙伴到現(xiàn)在還在用以下的%d%f%s ...等方式打印

age = int(input("Please input your age:"))
name = input("Please input your name:")
print("Ok,your name is %s, and your age is %d." % (name, age))

當(dāng)然,也不是說(shuō)這種方式不能用,但是我們有一種更加直觀并且方便的用法--print(F”“)。
以下是改進(jìn)后的代碼

age = int(input("Please input your age:"))
name = input("Please input your name:")
print(f"Ok,your name is {age}, and your age is {name}." )

這樣寫(xiě)的話是不是比較方便,但一定要記住引號(hào)前的 f 一定不能忘記。

"""
print用法
版本: v1.0
日期: 2019.03.25
作者: Catherine
python版本: 3.7
"""

print("用法1: ", end='')
print('hello, world!')
print()
print("用法2: ", end='')
print("你好,世界!")
print()
print("用法3: ", end='')
print('你好', '世界')
print()
print("用法4: ", end='')
print('hello', 'world', sep=', ', end='!')
print()
print()
print("用法5: ", end='')
print('goodbye, world', end='!\n')
print()
print("用法6: ")
s = 'Hello'
length = len(s)
print("The length of %s is %d" % (s, length))
print()
print("用法7: ")
pi = 3.141592653
print("字段寬10,精度3: ")
print('%10.3f' % pi)
print()
print("用*從后面的元組中讀取字段寬度或精度: ")
print("pi = %.*f" % (3, pi))
print()
print("用0填充空白: ")
print('%010.3f' % pi)
print()
print("左對(duì)齊: ")
print('%-10.3f' % pi)
print()
print("顯示正負(fù)號(hào): ")
print('%+f' % pi)
 print()
print("用法8: ")
print("print不換行: ")
for i in range(10):
 print(i, end='') # 0123456789
print()
print()
print("用法9: ")
list = ['床前明月光', '疑是地上霜', '舉頭望明月', '低頭思故鄉(xiāng)'] # 床前明月光-疑是地上霜-舉頭望明月-低頭思故鄉(xiāng)
print('-'.join(list))

Python 3的print是一個(gè)函數(shù),與Python2用法完全不一樣,現(xiàn)將Python3的print()函數(shù)用法滿匯總?cè)缦拢泄δ芫杀救擞H測(cè)。

print()輸出字符串用法。

例如:

print("輸出字符串")

print('用單引號(hào)輸出字符串')

python3的print()函數(shù)的用法圖文講解

單引號(hào)中輸出雙引號(hào),雙引號(hào)中輸出單引號(hào),轉(zhuǎn)義符輸出單、雙引號(hào)和轉(zhuǎn)義符操作方法。

#輸出單引號(hào)或雙引號(hào)方法

print("直接輸出一個(gè)單引號(hào)'a")

print('直接輸出一個(gè)雙引號(hào)"')

print('''直接輸出一個(gè)雙引號(hào)"''')

print('用轉(zhuǎn)義符號(hào)"\\"輸出單引號(hào)\'')

print("用轉(zhuǎn)義符號(hào)\"\\\"輸出單引號(hào)方法2\'")

print("以上例子包括了轉(zhuǎn)義符\\的輸出方法。")

python3的print()函數(shù)的用法圖文講解

數(shù)字的輸出方法,直接輸出和通過(guò)變量輸出,語(yǔ)句中均無(wú)引號(hào)。

#輸出數(shù)字的用法

#直接輸出數(shù)字 

print(100)

print(3.1415926)

#通過(guò)量變輸出數(shù)字

a=1.414

print(a)

python3的print()函數(shù)的用法圖文講解

變量的方法輸出字符串或數(shù)字。

str1="變量輸出用字符串"

num1="12.345"

print(str1)

print(num1)

python3的print()函數(shù)的用法圖文講解

輸出列表、元組和字典方法

L = [1,2,'a']   

print(L) 

t = (1,2,'a')   

print(t) 

d = {'a':1, 'b':2} 

print(d) 

python3的print()函數(shù)的用法圖文講解

print的格式化輸出

str2="以格式化方式輸出的(%s)有(%d)個(gè)字符" %('python',len('python'))

print(str2)

python3的print()函數(shù)的用法圖文講解

print()函數(shù)輸出換行控制。

python3的print()函數(shù)的用法圖文講解

一個(gè)print()語(yǔ)句換行輸出,在需要換行的位置加入"\n"換行符即可。

python3的print()函數(shù)的用法圖文講解

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI