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

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

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

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

函數(shù)參數(shù)使用舉例:

>>>num1=2
>>>str1='words'
>>>print(num1,str1)
2 words

默認(rèn)2與words之間有一個空格,輸出完成之后有一個回車
使用了@作為分割符

>>>print(num1,str1,sep='@')
2@words

使用&作為結(jié)尾

>>>print(num1,str1,sep='@',end='&')
2@words&

其實(shí)print函數(shù)還有一個參數(shù)file,通常print函數(shù)都是stdout方式輸出,但是如果需要將輸出結(jié)果重定向到一個文件可以使用file參數(shù)

f=open("d:\\new.txt","w")
for i in range(6):
print("new line",file=f)

此時在d盤更目錄下會自動創(chuàng)建一個new.txt文件,并打印6行new line在其中。

【Case1】

直接輸出打印內(nèi)容

>>>print("This is a new word.")
This is a new word.

如果內(nèi)容過長,可以使用\作為換行符

>>>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】

使用三引號作為格式打印

>>>str="""
This
is
a
new
word
.
"""
>>>print(str)
This
is
a
new
word
.

【Case4】

除了使用三引號作為格式化輸出,還有兩種格式化輸出的方式%和.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 單個字符
%b 二進(jìn)制整數(shù)
%o 八進(jìn)制整數(shù)
%x 十六進(jìn)制整數(shù)
%e 指數(shù) (基底寫為e)
%E 指數(shù) (基底寫為E)
%F 浮點(diǎn)數(shù),與上相同
%g 指數(shù)(e)或浮點(diǎn)數(shù) (根據(jù)顯示長度)
%G 指數(shù)(E)或浮點(diǎn)數(shù) (根據(jù)顯示長度



使用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 輸出一個橫向指標(biāo)符
/n 輸出一個換行符
/v 輸出一個縱向制表符
/f 輸出一個換頁
/r 輸出一個回車


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

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

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

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