課程
/后端開發
/Python
/初識Python
如上圖,1+1 執行結果是2,而 print 1+1 的結果也是 2,這兩個結果有什么不同?
2015-04-11
源自:初識Python
正在回答
基本上可以認為是一樣的,在python交互界面(也就是shell)中,如果輸入一個對象s,會調用print repr(s)來返回一個編譯器所看到的對s的一個字符串描述,一般情況下,repr()和str()的返回值是差不多的。但也有差異,你可以自己試試以下的例子:
>>> "hello the world\n"
'hello the world\n'
>>> print "hello the world\n"
hello the world
>>> repr("hello the world\n")
"'hello the world\\n'"
>>> print repr("hello the world\n")
Cokakin 提問者
舉報
學python入門視頻教程,讓你快速入門并能編寫簡單的Python程序
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2015-04-13
基本上可以認為是一樣的,在python交互界面(也就是shell)中,如果輸入一個對象s,會調用print repr(s)來返回一個編譯器所看到的對s的一個字符串描述,一般情況下,repr()和str()的返回值是差不多的。但也有差異,你可以自己試試以下的例子:
>>> "hello the world\n"
'hello the world\n'
>>> print "hello the world\n"
hello the world
>>> repr("hello the world\n")
"'hello the world\\n'"
>>> print repr("hello the world\n")
'hello the world\n'