日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python3與2中print有什么區(qū)別?

區(qū)別:python2中的print是一個(gè)關(guān)鍵字,而Python3里的print是一個(gè)函數(shù)。關(guān)鍵字用法“print 要打印的內(nèi)容”;函數(shù)用法“print(要打印的內(nèi)容)”。

創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、紅花崗網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁(yè)面制作成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為紅花崗等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

總地來(lái)說(shuō), Python2.7的print不是一個(gè)function,而Python3里的print是一個(gè)function。
兩都調(diào)用方式的主要區(qū)別如下:

print 'this is a string' #python2.7
print('this is a string') #python3

當(dāng)然,python2.7里你也可以用括號(hào)把變量括起來(lái), 一點(diǎn)都不會(huì)錯(cuò):

print('this is a string') #python2.7

但是python3將print改成function不是白給的:

1. 在python3里,能使用help(print)查看它的文檔了, 而python2不行:

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

2 . 在python3里,能更方便的使用輸出重定向
python2.7里,你需要以類似于C++的風(fēng)格完成重定向:

with open('print.txt', 'w') as f:
    print >> f, 'hello, python!'

在python3里:

with open('print.txt', 'w') as f:
    print('hello, python!', file = f)

file是python3 print新加的一個(gè)參數(shù)。 另一個(gè)很handy的參數(shù)是sep, 例如打印一個(gè)整數(shù)數(shù)組, 但你想用星號(hào)而不是空格連接。python2時(shí)可能需要寫(xiě)一個(gè)循環(huán)來(lái)完成, python3里這樣就行了:

a = [1, 2, 3, 4, 5]
print(*a, sep = '*')

最后, 如果想在python2.7里使用python3的print,只需要在第一句代碼前加入:

from __future__ import print_function

注意, from __future__ import ...一類的語(yǔ)句一定要放在代碼開(kāi)始處。

推薦課程:python教程之Django視頻教程


網(wǎng)站標(biāo)題:創(chuàng)新互聯(lián)Python教程:python3與2中print有什么區(qū)別?
標(biāo)題鏈接:http://www.dlmjj.cn/article/cddddeo.html