开发者

function print in python shell

开发者 https://www.devze.com 2023-03-12 18:47 出处:网络
Can anyone explain me difference in python shell between output variable through \"print\" and when I just write variab开发者_开发百科le name to output it?

Can anyone explain me difference in python shell between output variable through "print" and when I just write variab开发者_开发百科le name to output it?

>>> a = 5
>>> a
5
>>> print a
5
>>> b = 'some text'
>>> b
'some text'
>>> print b
some text

When I do this with text I understand difference but in int or float - I dont know.


Just entering an expression (such as a variable name) will actually output the representation of the result as returned by the repr() function, whereas print will convert the result to a string using the str() function.>>> s = "abc"

Printing repr() will give the same result as entering the expression directly:

>>> "abc"
'abc'
>>> print repr("abc")
'abc'


Python's shell always returns the last value evaluated. When a is 5, it evaluates to 5, thus you see it. When you call print, print outputs the value (without quotes) and returns nothing, thus nothing gets produced after print is done. Thus, evaluating b results in 'some test' and printing it just results in some text.

0

精彩评论

暂无评论...
验证码 换一张
取 消