Python Forum
Why does absence of print command outputs quotes in function? - 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: Why does absence of print command outputs quotes in function? (/thread-35971.html)



Why does absence of print command outputs quotes in function? - Mark17 - Jan-04-2022

Hi all,

For this function:

def two_word_shout(word1,word2):
    return word1+'!'*3+' '+word2+'!'*3
two_word_shout('congratulations','you') outputs 'congratulations!!! you!!!' whereas enclosing in print() generates the same output without the quotes. Why the difference, exactly?


RE: Why does absence of print command outputs quotes in function? - ibreeden - Jan-04-2022

PyDev console: starting.
Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0] on linux
def two_word_shout(word1,word2):
    return word1 + '!' * 3 + ' ' + word2 + '!' * 3
two_word_shout('congratulations','you')
'congratulations!!! you!!!'
print(two_word_shout('congratulations','you'))
congratulations!!! you!!!
This happens only in interactive mode. The first time you do not say what you want with the result, so Python shows it to you and also shows quotation marks to let you know it is a string type.
The second time you print the value and the print function just prints the result without letting you know the type of what you are printing.


RE: Why does absence of print command outputs quotes in function? - ndc85430 - Jan-04-2022

You also need to realise that printing and returning are not at all the same. The former writes the value to the standard output stream, so the print function returns None. In the latter case, the value returned is made available to the caller and can hence be used in further computations.