Python Forum
output while using return instead of print - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: output while using return instead of print (/thread-25960.html)



output while using return instead of print - muza - Apr-17-2020

when i create a function with return value, it outputs with single quotes example:
def show(string = "my output"):
    return string
show()
Output:
'my output'
why this happens?
can i remove this quots so it looks like
Output:
my output



RE: output while using return instead of print - buran - Apr-17-2020

You sort of mix two things.
First of all you must understand that there are different ways to execute python code.
Interactive mode (i.e. python shell), where each line is evaluated immediatelly and result is displayed. The code is not saved and is lost when you close the shell. This is when you have >>> at the start of the line. From your description, this is what you do. show()calls your function, it returns string my output and the representation of this string is displayed. It's a string and that's why it has quotes. Note that in python we have single quotes ', double quotes " and triple quotes ''' or """. The python shell is used to experiment and test, but not the way to write code that persist.
The other way to write your code in a .py file, save it and execute it. If you do this, your code will display nothing, because you call your function, it returns string and you do nothing with it, it is lost. You can assign it to name for later use in the code or just print it, e.g.
def show(string = "my output"):
    return string
print(show())
As a side note - don't use string as parameter in the function. string is module from standard library and you shadow it.


RE: output while using return instead of print - muza - Apr-23-2020

Got it clearly. Thanks!