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.
BashBedlam and Mark17 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
  PDFminer outputs unreadable text during conversion from PDF to TXT Gromila131 6 1,871 Aug-06-2024, 08:20 AM
Last Post: Pedroski55
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 12 4,667 Apr-12-2024, 11:51 AM
Last Post: deanhystad
  print doesnt work in a function ony 2 1,140 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  problem with print command in super() akbarza 5 1,927 Feb-01-2024, 12:25 PM
Last Post: deanhystad
  format json outputs ! evilcode1 3 2,741 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  Formatting outputs created with .join command klairel 2 1,610 Aug-23-2023, 08:52 AM
Last Post: perfringo
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 2,225 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to print variables in function? samuelbachorik 3 1,900 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  How to print the output of a defined function bshoushtarian 4 2,364 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  I have written a program that outputs data based on GPS signal kalle 1 2,125 Jul-22-2022, 12:10 AM
Last Post: mcmxl22

Forum Jump:

User Panel Messages

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