日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python内置输入函数_python内置函数 print()

發布時間:2025/3/15 python 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python内置输入函数_python内置函数 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 withbinary 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)

參數解釋

value

需要打印的內容

...

可以輸入多個內容

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

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 "", line 1, in

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

總結

以上是生活随笔為你收集整理的python内置输入函数_python内置函数 print()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。