-2
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?

Nacka
  • 5
  • 1
  • 6
  • The function `print_name` returns `None` since no return is specified. You print the return value in your final print statement. – Primusa Feb 10 '19 at 19:32
  • 2
    `None` isn't the output; it's the return value. The two concepts are *completely* different. The confusion is that the interactive interpreter, when evaluating a function call, prints its return value to standard output. – chepner Feb 10 '19 at 19:32

5 Answers5

2

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.

1

Because you are print the method print, the return should be name, not print(name).

Fernandoms
  • 444
  • 3
  • 13
1

Your function is not returning anything that's why it is giving None. An non-returning function is returned with None.

bhalu007
  • 131
  • 1
  • 10
1

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'))
Heyran.rs
  • 501
  • 1
  • 10
  • 20
0

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".

Michael T
  • 370
  • 4
  • 16