def print_name(name):
print(name)
print(print_name('Annabel Lee'))
Why do I get the following output:
Annabel Lee
None
More precisely, from where does the word None
come from?
def print_name(name):
print(name)
print(print_name('Annabel Lee'))
Why do I get the following output:
Annabel Lee
None
More precisely, from where does the word None
come from?
You have two calls to print
: one inside print_name
and another outside the function's scope.
The one inside print_name()
prints the name
passed in. The one on the outside prints what the function print_name
returns - which is None
as you have no return
statement. Presuming you want only one printed output, you'd return
it instead of printing it in the function:
def print_name(name):
return name
print(print_name('Annabel Lee'))
Or just call print_name
without wrapping it in a print
function.
Because you are print the method print
, the return should be name, not print(name)
.
Your function is not returning anything that's why it is giving None. An non-returning function is returned with None.
Your function prints the name and you don't need to use print()
again.
def print_name(name):
print(name)
print_name('Annabel Lee')
If don't use return
in a function, it returns None
by default. Your code was correct if your function was like this:
def print_name(name):
return name
print(print_name('Annabel Lee'))
The print()
function evaluates the arguments in the parentheses and print the result.
print(1+1)
will print "2", since the result of 1+1 is 2. Just like that, print("abcd".upper())
will print "ABCD".
When you call print(print_name('Annabel Lee'))
, the print function tries to evaluate the argument, in this case, print_name('Annabel Lee')
. Since the print_name()
function does not return any value (it just prints 'Annabel Lee'), the returned value is None. This is why the print()
function print "None".