Python3內置函數——print


英文文檔:

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 like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, 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 a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.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

返回值

<class 'NoneType'> 返回相應的輸出結果。

函數說明

打印相應的內容。

 

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 環境)

1 >>> print(1,'2',[3.40,'5'],(6,[7,8],'9'))            #參數缺省
2 1 2 [3.4, '5'] (6, [7, 8], '9')
3 >>> print(1, '2', 3.00, sep = '|', end = '\nline2')  #使用'|'作為分隔符,'\nline2'為結束符
4 1|2|3.0                                             
5 line2
6 >>> print(1, '2', 3.00, sep = '', end = '')          #這里需要注意"'2'"輸出后為"2"
7 123.0

 

 范例2:通過更改file參數打印內容到文件(Python 3.6.2 shell 環境)

1 >>> with open(r'G:\temp.txt', 'w') as demo:  
2 print(1, 2, 3, sep = ',', end = '\n', file = demo)
3 
4 
5 >>>

  G盤下被新建txt文檔’temp.txt’,其內容為:

  1,2,3

  line2

 

范例3:格式化輸出(Python 3.6.2 Shell 環境)

 1 >>> pi = 3.141592653
 2 >>> print('%f' % pi) #默認願長度為寬度,保留小數點后六位  3 3.141593  4 >>> print('%+f' % pi) #顯示正負號  5 +3.141593  6 >>> print('%4f' % pi) #寬度設小不會丟失精度  7 3.141593  8 >>> print('%10.3f' % pi) #寬度10,保留小數點后2位,右對齊  9 *****3.142 #用*表示空格 10 >>> print('%-10.3f' % pi) #寬度10,保留小數點后2位,左對齊 11 3.142***** 12 >>> print('%010.3f' % pi) #用0填充空白 13 000003.142 14 >>> print('%e' % pi) #科學計數法格式化輸出 15 3.141593e+00 16 >>> print('%.2E' % (pi * 10000)) 17 3.14E+04 18 >>> print('a%cc' % 'b') #單字符格式化輸出 19 abc 20 >>> print('%c' % 'abc') #單字符格式輸入多字符時的報錯信息 21 Traceback (most recent call last): 22 File "<pyshell#12>", line 1, in <module> 23 print('%c' % 'abc') 24 TypeError: %c requires int or char 25 >>> print('a%sd' % 'bc') #格式化輸出字符串 26 abcd 27 >>> print('a%rd' % 'bc') #格式化輸出字符串,保留“’’” 28 a'bc'd 29 >>>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM