Python基礎(chǔ)print()的使用

Print:打印標(biāo)準(zhǔn)輸出的函數(shù)
格式:print()
print()函數(shù)有三個(gè)參數(shù)

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、云巖網(wǎng)站維護(hù)、網(wǎng)站推廣。

  1. value用戶需要輸出的信息
  2. sep多個(gè)要輸出信息之間的分隔符,默認(rèn)是空格
  3. end輸出信息結(jié)尾處的默認(rèn)添加的符號(hào),默認(rèn)為換行符

函數(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ē)


新聞標(biāo)題:Python基礎(chǔ)print()的使用
本文路徑:http://muchs.cn/article48/pphcep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站自適應(yīng)網(wǎng)站、建站公司、App設(shè)計(jì)、軟件開(kāi)發(fā)、外貿(mào)建站

廣告

聲明:本網(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)

成都seo排名網(wǎng)站優(yōu)化