'Python Struggling to Understand difference between return and print
I'm working on creating a function that returns the last_name, followed by a comma, a space, first_name another space, and finally last_name.
The below code gives me the correct answer:
def introduction(first_name, last_name):
return last_name + ", " + first_name + " " + last_name
print(introduction("James", "Bond"))
Bond, James Bond
However, if I use print, I get the following:
def introduction(first_name, last_name):
print(last_name + ", " + first_name + " " + last_name)
print(introduction("James", "Bond"))
Bond, James Bond
None
Angelou, Maya Angelou
None
Where does the none come from when using the print instead of return? I have looked around and I can't seem to tell which to use.
Solution 1:[1]
None is what is returned by the function "print". That is, print sends something to stdout and then returns a None. You can verify this by explicitly returning the value and checking:
x = print('something')
print(x)
You introduction statement is returning a None, hence your statement
Print(introduction('James','Bond'))
First runs the introduction(,) which itself has a print statement that prints the name, but then returns a None, from which the print above prints.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Bobby Ocean |