Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does Python Print return None
#1
Hey all. I'm studying for a final. Wondering why using 
print(print('hello') return
Hello
None
Can't wrap my head around it. 

Thanks everyone.

-Steven
Quote
#2
First of all, your code doesn't do that. You code has a syntax error in it, and even if it didn't you can't print a lower case hello and get an upper case Hello printed.

Why does it return None? It has to return something, because every Python function does. Now think about this situation:

>>> print('Hello world!')
Hello world!
That's what happens when print returns None, because None does not have a repr. So it prints the string to print, and then prints the repr of the return value, which is nothing. Say print returned the string it printed. Then you would get this:

>>> print('Hello world!')
Hello world!
'Hello world!'
It would display 'Hello world!' twice, once without quotes, and once with quotes (the repr of the string). That would be silly. And that's why print returns None.
Craig "Ichabod" O'Brien - xenomind.com
Buddhist, biker, poet, coder, theist
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
Well, I mean that if I type print(print("hello")) at the python command line, it literally returns hello and then on the next line, slightly indented, None.
Quote
#4
Quote:>>> print(print("hello"))
hello
None
All functions return None if not returning anything else. And in python3.x print is a function. The inner hello function runs, and prints hello. Then returns...which returns None. So the outer print becomes
Quote:print(None)
Which that print outputs the return value of None.
*Describe the environment in which it occurs.                                    *Describe the symptoms of your problem clearly.
*Describe the research you did to try and understand the problem.      *Describe the goal, not the step. 
*Use meaningful, specific subject headers                                          *Write in clear, grammatical, correctly-spelled language
*Describe the problem's symptoms, not your guesses                         *Describe your problem's symptoms in chronological order
*Describe the diagnostic steps you took to try and pin down the problem yourself.
*Describe any possibly relevant recent changes in your computer or software configuration. 
*Provide a way to reproduce the problem in a controlled environment.
Quote
#5
Thank you!
Quote
#6
first of all, your code:
print(print('hello') return
is missing a closing parentheses and should be
print(print('hello'))

There is no need for a return statement. and it shouldn't be included (but it won't hurt the code)
Quote
#7
What should it return? The text that was being printed to screen/file/network? A success/failure code?
It returns None, because it doesn't really make sense for it to return anything else. It does a thing, it doesn't change a thing.
It's dangerous to go alone!  Take this: https://docs.python.org/3/library/index.html
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  given 2 base 20 numbers write a program to subtract second from the first and return dp_tisha 2 235 Jul-06-2017, 09:22 PM
Last Post: nilamo
  Calling a function to return a list of percentages Liquid_Ocelot 7 392 Mar-25-2017, 01:20 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)