I don't quite understand output received from:
print(print(print('aaa')))
aaa
None
None
First aaa is clear. But I thought that second print(aaa) will throw an error as variable aaa is not defined...
The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.
print(' ') will print a space character and a newline; while print('') (or print() ) will only print the newline. It doesn't take away anything. Thank you very much! Now I understand why the output looked the way that it did.
The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.
The basic way to do output is the print statement. To end the printed line with a newline, add a print statement without any objects. This will print to any object that implements write(), which includes file objects.
print(print('aaa'))
The outer print
will receive as argument not what inner print
printed to stdout, but what inner print
returned. And print
function never returns anything (equivalent to returning None
). That's why you see this output.
Here is an example which does the same thing, and you will understand it better:
def f():
print('Hello')
print(f())
Outputs:
Hello
None
None
is at the end because you are basically doing print(print('Hello'))
, print
writes something in the python interpreter and also when you do type(print())
it outputs: <class 'NoneType'>
So this part is print(None)
.
So that's why the output of print(print(print('aaa')))
includes None
's
First we just split our code
>>>a = print('aaa')
aaa
>>>b = print(a)
None
>>>print(b)
None
Now you understand !! (python 3)
print
prints on the standard output and returns None
. Your second print
statement gets result of first print
which is None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With