Python——python2的print和python3的print()


本文轉載自:https://www.cnblogs.com/kaitoex/p/6085606.html

 

python2.x和3.x中的輸出語句有着明顯不同

2.x中的print不是個函數,輸出格式如下

1 Python 2.7.12+ (default, Aug  4 2016, 20:04:34) 
2 [GCC 6.1.1 20160724] on linux2
3 Type "help", "copyright", "credits" or "license" for more information.
4 >>> print "There is only %d %s in the sky."%(1,'sun')
5 There is only 1 sun in the sky.

3.x中的print成了函數,輸出格式如下

1 Python 3.5.2+ (default, Aug  5 2016, 08:07:14) 
2 [GCC 6.1.1 20160724] on linux
3 Type "help", "copyright", "credits" or "license" for more information.
4 >>> print("There is only %d %s in the sky."%(1,'sun'))
5 There is only 1 sun in the sky.

為什么要做出這樣的變化,主要原因有以下幾點:

1.print不是函數,不能使用help(),對使用者不方便。

python2中help(print)會報錯。

1 >>> help(print)
2   File "<stdin>", line 1
3     help(print)
4              ^
5 SyntaxError: invalid syntax

python3中,可以使用help(print),清楚的看到print的參數。

 1 Help on built-in function print in module builtins:
 2 
 3 print(...)
 4     print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
 5     
 6     Prints the values to a stream, or to sys.stdout by default.
 7     Optional keyword arguments:
 8     file:  a file-like object (stream); defaults to the current sys.stdout.
 9     sep:   string inserted between values, default a space.
10     end:   string appended after the last value, default a newline.
11     flush: whether to forcibly flush the stream.
12 (END)

2.從上面的help(print)中我們也可以看到在print()中的兩個重要參數,sep和end。這兩個參數使print()相比print多了兩個新功能,自定義間隔符(默認空格)和結束符(默認回車)。

1 >>> print("123","456","789")
2 123 456 789
3 >>> print("123","456","789",sep='-')
4 123-456-789
1 >>> x=1024
2 >>> print(t)
3 256
4 >>> print(t,end=" end")
5 256 end>>> 
6 >>> print(t,end=" end\n")
7 256 end

3.print()重定向輸出文件更加方便。

2.x需要print>>重定向輸出,感覺代碼很混亂。

1 >>> out=open("test.txt","w")
2 >>> print>>out,"123"

3.x中輸出文件成了一個參數,使用更方便。

1 >>> out=open("test.txt","w")
2 >>> print("123",file=out)

4.python2.x中print語句的格式化輸出源自於C語言的格式化輸出,這種語法對於C這種靜態語言比較適用,但是對於擁有很多先進數據結構的python來說就有點力不從心了。python的元組,列表,字典,集合等不適合用這種結構表示,這些數據結構大多元素用下標表示,在這種結構中寫出來很混亂。python3.x的print()函數提供了有點類似C#(不知道這么說對不對)中的格式化輸出函數format()。另外print()也兼容原來的格式化輸出方式。

1 >>> print("%s is %s."%('Aoko','good'))
2 Aoko is good.

format()讓輸出格式更清晰。

1 >>> print("{0} is {1}.".format('Aoko','good'))
2 Aoko is good.

format()支持數組下標,使python中的一些數據結構輸出更加方便。

1 >>> name=["Kaito",5]
2 >>> print("{0[0]} has {0[1]} dollars.".format(name))
3 Kaito has 5 dollars.

format()下的格式限定符,和原來的差不多。

1 >>> x=5.6
2 >>> print("{0:4f}".format(x))
3 5.600000

由此看來,print()相比print還是有很大進步的。

 


再看print()函數:

參考print的官方文檔

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.
  • 在python中,print默認向屏幕輸出指定的文字,例如:

    >>>print('hello,world')
    hello world

  • print的完整格式為print(objects,sep,end,file,flush),其中后面4個為可選參數

    1. sep
      在輸出字符串之間插入指定字符串,默認是空格,例如:
      >>>print("a","b","c",sep="**")
      a**b**c
    2. end
      print輸出語句的結尾加上指定字符串,默認是換行(\n),例如:
      >>>print("a",end="$")
      a$
      print默認是換行,即輸出語句后自動切換到下一行,對於python3來說,如果要實現輸出不換行的功能,那么可以設置end=''(python2可以在print語句之后加“,”實現不換行的功能)
    3. file
      將文本輸入到file-like對象中,可以是文件,數據流等等,默認是sys.stdout
      >>>f = open('abc.txt','w')
      >>>print('a',file=f)
    4. flush
      flush值為True或者False,默認為Flase,表示是否立刻將輸出語句輸入到參數file指向的對象中(默認是sys.stdout)例如:
      >>>f = open('abc.txt','w')
      >>>print('a',file=f)
      可以看到abc.txt文件這時為空,只有執行f.close()之后才將內容寫進文件。
      如果改為:
      >>>print('a',file=f,flush=True)
      則立刻就可以看到文件的內容


 
參考資料:
[1]. 博客園:https://www.cnblogs.com/kaitoex/p/6085606.html
[2]. 幾度夏的博客園:https://www.cnblogs.com/jiduxia/p/7492037.html
[3]. 關於Python字符串格式化輸出,可以參考:https://www.cnblogs.com/oddcat/articles/9630332.html 
 
 


免責聲明!

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



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