Print:打印標(biāo)準(zhǔn)輸出的函數(shù)
格式:print()
print()函數(shù)有三個(gè)參數(shù)
函數(shù)參數(shù)使用舉例:
>>>num1=2 >>>str1='words' >>>print(num1,str1) 2 words
默認(rèn)2與words之間有一個(gè)空格,輸出完成之后有一個(gè)回車(chē)
使用了@作為分割符>>>print(num1,str1,sep='@') 2@words
使用&作為結(jié)尾
>>>print(num1,str1,sep='@',end='&') 2@words&
其實(shí)print函數(shù)還有一個(gè)參數(shù)file,通常print函數(shù)都是stdout方式輸出,但是如果需要將輸出結(jié)果重定向到一個(gè)文件可以使用file參數(shù)
f=open("d:\\new.txt","w") for i in range(6): print("new line",file=f)
此時(shí)在d盤(pán)更目錄下會(huì)自動(dòng)創(chuàng)建一個(gè)new.txt文件,并打印6行new line在其中。
【Case1】
直接輸出打印內(nèi)容
>>>print("This is a new word.") This is a new word.
如果內(nèi)容過(guò)長(zhǎng),可以使用\作為換行符
>>>print("This is a new \ word") This is a new word
【Case2】
使用變量打印內(nèi)容
>>> a="This is a new word." >>> print(a) This is a new word.
【Case3】
使用三引號(hào)作為格式打印
>>>str=""" This is a new word . """ >>>print(str) This is a new word .
【Case4】
除了使用三引號(hào)作為格式化輸出,還有兩種格式化輸出的方式%和.format
使用%占位符格式化打印
主要使用的占位符有:
%s 字符串
%r 字符串
%d 十進(jìn)制整數(shù)
%i 十進(jìn)制整數(shù)
%f 浮點(diǎn)數(shù)>>>str1='world' >>>print('hello %s' %'world') hello world
>>>print('hello %s' %str1) hello world
>>>str='abc' >>>print("%s" %str) abc >>> print("%s" %str.title()) Abc
>>>print('hello %r' %str1) hello 'world'
>>>num1=300 >>>print ('the number is','%d' %num1) the number is 300
>>>print ('the number is','%i' %num1) the number is 300
>>> num2=30 >>> name='John' >>> print('He is %s, he is %d years old.' %(name,num2)) He is John, he is 30 years old.
其它占位符:
%c 單個(gè)字符
%b 二進(jìn)制整數(shù)
%o 八進(jìn)制整數(shù)
%x 十六進(jìn)制整數(shù)
%e 指數(shù) (基底寫(xiě)為e)
%E 指數(shù) (基底寫(xiě)為E)
%F 浮點(diǎn)數(shù),與上相同
%g 指數(shù)(e)或浮點(diǎn)數(shù) (根據(jù)顯示長(zhǎng)度)
%G 指數(shù)(E)或浮點(diǎn)數(shù) (根據(jù)顯示長(zhǎng)度使用format()方法格式化打印
>>> str=''' Username={uname} Id={id} Phone={tel} '''.format(uname='Jone',id='123',tel='138***') >>> print(str) Username=Jone Id=123 Phone=138***
>>> str2="{0}".format("abc") >>> print(str2) abc
>>> str=''' username={0} Id={1} Phone={2} '''.format('Jone','123','138***') >>> print(str) username=Jone Id=123 Phone=138***
【Case5】
轉(zhuǎn)義字符
常用轉(zhuǎn)義字符:
/t 輸出一個(gè)橫向指標(biāo)符
/n 輸出一個(gè)換行符
/v 輸出一個(gè)縱向制表符
/f 輸出一個(gè)換頁(yè)
/r 輸出一個(gè)回車(chē)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁(yè)題目:Python基礎(chǔ)print()的使用-創(chuàng)新互聯(lián)
當(dāng)前地址:http://aaarwkj.com/article0/ihiio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站導(dǎo)航、網(wǎng)站收錄、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(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)