python2 與python3的print區別小結,python2python3

來源:互聯網
上載者:User

python2 與python3的print區別小結,python2python3

在Python2和Python3中都提供print()方法來列印資訊,但兩個版本間的print稍微有差異

主要體現在以下幾個方面:

1.python3中print是一個內建函數,有多個參數,而python2中print是一個文法結構;

2.Python2列印時可以不加括弧:print 'hello world', Python3則需要加括弧 print("hello world")

3.Python2中,input要求輸入的字串必須要加引號,為了避免讀取非字串類型發生的一些行為,不得不使用raw_input()代替input()

1. python3中,或許開發人員覺得print同時具有兩重身份有些不爽,就只留了其中函數的身份:

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

從上面的方法原型可以看出,

①. print可以支援多個參數,支援同時列印多個字串(其中...表示任意多個字串);

②. sep表示多個字串之間使用什麼字元串連;

③. end表示字串結尾添加什麼字元,指點該參數就可以輕鬆設定列印不換行,Python2.x下的print語句在輸出字串之後會預設換行,如果不希望換行,只要在語句最後加一個“,”即可。但是在Python 3.x下,print()變成內建函數,加“,”的老方法就行不通了。

>>> print("python", "tab", ".com", sep='') pythontab.com >>> print("python", "tab", ".com", sep='', end='') #就可以實現列印出來不換行 pythontab.com 

Python2列印時可以不加括弧:print 'hello world', Python3則需要加括弧 print("hello world")

python3中print必須使用括弧,因為它就是一個函數。

總地來說, Python2.7的print不是一個function,而Python3裡的print是一個function。
兩都調用方式的主要區別如下:

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

當然,python2.7裡你也可以用括弧把變數括起來, 一點都不會錯:

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++的風格完成重新導向:

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新加的一個參數。 另一個很handy的參數是sep, 例如列印一個整數數組, 但你想用星號而不是空格串連。python2時可能需要寫一個迴圈來完成, python3裡這樣就行了:

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

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

from __future__ import print_function

注意, from __future__ import ...一類的語句一定要放在代碼開始處。

print輸出差異:同一段代碼

#/usr/bin/env python#coding:utf-8for i in range(1,10):    for j in range(1,10):        for k in range(1,10):            if(i != k)and(i != j)and(k != j):                print(i,j,k)

pyhon2的輸出為 i,j,k
python3的輸出為 i j k
python3的輸出直接屏蔽了逗號。

另python2 的print後序可不添加括弧。
phthon3必須添加括弧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.