溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python輸出函數是哪個

發布時間:2020-11-23 11:14:46 來源:億速云 閱讀:255 作者:小新 欄目:編程語言

了解python輸出函數是哪個?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家帶來的參考內容,讓我們一起來看看吧!

在編程實踐中,print 的使用頻率非常高,特別是程序運行到某個時刻,要檢測產生的結果時,必須用 print 來打印輸出。

關于 print 函數,前面很多地方已經提及過,可用于寫入標準輸出?,F在,是時候該深入了。

注意:這里強調的是“print 函數”,而不是“print 語句”。

深入print

在 Python 2.x 中,print 是一個語句,但是在 Python 3.x 中,它是一個函數。如果 2.x 和 3.x 都使用過,你就會發現差異有多么大。

進入 3.x 的交互式 shell,嘗試使用“print 語句”:

[wang@localhost ~]$ python
Python 3.5.2 (default, Mar 29 2017, 11:05:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> print 'Python'
...
SyntaxError: Missing parentheses in call to 'print'

對于大多數人來說,這個錯誤信息再熟悉不過了。正如上面所提到的那樣,print 是 3.x 中的一個函數,與其他函數一樣,參數應該被圓括號括起來

>>> print('Python')
Python

print函數

要了解 print 函數的用途,可以使用 help() 來尋求幫助:

>>> help(print)
...
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

將對象輸出到文本流文件,由 sep 分開,然后以 end 結束。如果 sep、end、file 和 flush 出現,則必須以關鍵字參數的形式指定。

不使用關鍵字參數

print 函數可以打印任意數量的值(value1, value2, …),這些值由逗號分隔。

>>> age = 18
>>> 
>>> print('age', age)
age 18

很容易發現,兩個值之間有一個分隔符 - 空格(默認值),這取決于 sep。

分隔符

如果要重新定義分隔符,可以通過 sep 來指定。

>>> print('age', age, sep='')  # 去掉空格
age18
>>> 
>>> print('www', 'python', 'org', sep='.')  # 以 . 分割
www.python.org

感謝各位的閱讀!看完上述內容,你們對python輸出函數是哪個大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女