Python Forum
Why does absence of print command outputs quotes in function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does absence of print command outputs quotes in function?
#1
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?
Reply
#2
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.
Mark17 and BashBedlam like this post
Reply
#3
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.
Mark17 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 909 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  print doesnt work in a function ony 2 311 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  problem with print command in super() akbarza 5 605 Feb-01-2024, 12:25 PM
Last Post: deanhystad
  format json outputs ! evilcode1 3 1,758 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  Formatting outputs created with .join command klairel 2 636 Aug-23-2023, 08:52 AM
Last Post: perfringo
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,105 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to print variables in function? samuelbachorik 3 916 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  How to print the output of a defined function bshoushtarian 4 1,318 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  I have written a program that outputs data based on GPS signal kalle 1 1,182 Jul-22-2022, 12:10 AM
Last Post: mcmxl22
  return vs. print in nested function example Mark17 4 1,757 Jan-04-2022, 06:02 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020