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") # 打開文件,以便寫入
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}." )
這樣寫的話是不是比較方便,但一定要記住引號(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))
當(dāng)前文章:python3的print()函數(shù)的用法圖文講解-創(chuàng)新互聯(lián)
分享鏈接:http://www.yijiale78.com/article6/csihog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、電子商務(wù)、手機(jī)網(wǎng)站建設(shè)、定制開發(fā)、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容