Python print function

Can someone explain to me the difference in the python shell between the output variable via "print" and when I just write the variable 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 the difference, but in int or float - I don't know.

+4
source share
2 answers

A simple occurrence of an expression (for example, the name of a variable) will actually output a representation of the result returned by the repr() function, whereas print converts the result to a string using the str() function. β†’> s = "abc"

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

 >>> "abc" 'abc' >>> print repr("abc") 'abc' 
+10
source

The Python shell always returns the last evaluated value. When a is 5, it evaluates to 5, so you see it. When you call print , print prints the value (without the quotes) and returns nothing, so after print nothing happens. Thus, evaluating b leads to 'some test' , and printing it simply leads to some text .

+2
source

All Articles