python3內建函式 print

2021-09-05 03:39:20 字數 4126 閱讀 4429

** 

print(*objects, sep=』 『, end=』\n』, file=sys.stdout, flush=false)

print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.

all non-keyword arguments are converted to strings likestr()does and written to the stream, separated by sep and followed by end. both sep and end must be strings; they can also benone, which means to use the default values. if no objects are given,print()will just write end.

the file argument must be an object with awrite(string)method; if it is not present ornone,sys.stdoutwill be used. since printed arguments are converted to text strings,print()cannot be used with binary mode file objects. for these, usefile.write(...)instead.

whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

changed in version 3.3: added the flush keyword argument.

函式資訊**

函式原型

print(value,...,sep=『 』, end=』\n』, file=sys.stdout, flush=false)

引數解釋

sep分隔符,預設為空格;

end輸出結束時補充該引數所指定的字串,預設為換行符;

file

定義流輸出的檔案,預設為標準的系統輸出sys.stdout,可以重定義為別的檔案;

flush

是否立即把內容輸出到流檔案,不作快取,預設為false。

返回值返回相應的輸出結果。

函式說明

列印相應的內容。

print函式的格式化輸出

格式化輸出:

1) %字元:標記轉換說明符的開始

2) 轉換標誌:-表示左對齊;+表示在轉換值之前要加上正負號;「」(空白字元)表示正數之前保留空格;0表示轉換值若位數不夠則用0填充

3) 最小字段寬度:轉換後的字串至少應該具有該值指定的寬度。如果是*,則寬度會從值元組中讀出。

4) 點『.』後跟精度值:如果轉換的是實數,精度值就表示出現在小數點後的位數。如果轉換的是字串,那麼該數字就表示最大字段寬度。如果是*,那麼精度將從元組中讀出

5) 字串格式化轉換型別

含義d,i

帶符號的十進位制整數

o不帶符號的八進位制

u不帶符號的十進位制

x不帶符號的十六進製制(小寫)

x不帶符號的十六進製制(大寫)

e科學計數法表示的浮點數(小寫)

e科學計數法表示的浮點數(大寫)

f,f十進位制浮點數

g如果指數大於-4或者小於精度值則和e相同,其他情況和f相同

g如果指數大於-4或者小於精度值則和e相同,其他情況和f相同

c單字元(接受整數或者單字元字串)

r字串(使用repr轉換任意python物件)

s字串(使用str轉換任意python物件)

範例1:基本的列印輸出(python 3.6.2 shell 環境)

>>> print(1,'2',[3.40,'5'],(6,[7,8],'9'))            #引數預設

1 2 [3.4, '5'] (6, [7, 8], '9')

>>> print(1, '2', 3.00, sep = '|', end = '\nline2') #使用'|'作為分隔符,'\nline2'為結束符

1|2|3.0

line2

>>> print(1, '2', 3.00, sep = '', end = '') #這裡需要注意"'2'"輸出後為"2"

123.0

範例2:通過更改file引數列印內容到檔案(python 3.6.2 shell 環境)

>>> with open(r'g:\temp.txt', 'w') as demo:  

print(1, 2, 3, sep = ',', end = '\n', file = demo)

>>>

g盤下被新建txt文件』temp.txt』,其內容為:

1,2,3

line2

>>> pi = 3.141592653

>>> print('%f' % pi) #預設願長度為寬度,保留小數點後六位

3.141593

>>> print('%+f' % pi) #顯示正負號

+3.141593

>>> print('%4f' % pi) #寬度設小不會丟失精度

3.141593

>>> print('%10.3f' % pi) #寬度10,保留小數點後2位,右對齊

*****3.142 #用*表示空格

>>> print('%-10.3f' % pi) #寬度10,保留小數點後2位,左對齊

3.142*****

>>> print('%010.3f' % pi) #用0填充空白

000003.142

>>> print('%e' % pi) #科學計數法格式化輸出

3.141593e+00

>>> print('%.2e' % (pi * 10000))

3.14e+04

>>> print('a%cc' % 'b') #單字元格式化輸出

abc>>> print('%c' % 'abc') #單字元格式輸入多字元時的報錯資訊

traceback (most recent call last):

file "", line 1, in print('%c' % 'abc')

typeerror: %c requires int or char

>>> print('a%sd' % 'bc') #格式化輸出字串

abcd

>>> print('a%rd' % 'bc') #格式化輸出字串,保留「』』」

a'bc'd

>>>

Python3內建函式

1 兩個列表可以相加 collections.counter計數器,計算列表中每項出現的次數,並返回字典型別,其中元素作為key,其計數作為value。當所訪問的鍵不存在時,返回0,而不是keyerror 否則返回它的計數。其中負號表示降序 預設公升序 與reverse true相同 4 數值的除法...

Python3 內建函式

返回乙個數的絕對值。實參可以是整數或浮點數。如果實參是乙個複數,返回它的模。返回 x 的 y 次冪 如果 z 存在,則對 z 取餘 比直接pow x,y z計算更高效 兩個引數形式的pow x,y 等價於冪運算子 x y。引數必須為數值型別。對於混用的運算元型別,則適用二元算術運算子的型別強制轉換規...

Python3 內建函式

abs 函式 函式返回 x 數字 的絕對值 python dict 函式 dict 函式用於建立乙個字典。dict 建立空字典 dict a a b b t t 傳入關鍵字 dict zip one two three 1,2,3 對映函式方式來構造字典 dict one 1 two 2 three...