Python Forum
output while using return instead of print
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
output while using return instead of print
#1
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
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Got it clearly. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,075 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to output one value per request of the CSV and print it in another func? Student44 3 1,323 Nov-11-2022, 10:45 PM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,279 Sep-08-2022, 01:44 PM
Last Post: deanhystad
Sad Want to Save Print output in csv file Rasedul 5 10,904 Jan-11-2022, 07:04 PM
Last Post: snippsat
  return vs. print in nested function example Mark17 4 1,738 Jan-04-2022, 06:02 PM
Last Post: jefsummers
Photo print output none 3lnyn0 4 1,820 Nov-01-2021, 08:46 PM
Last Post: 3lnyn0
  output correction using print() function afefDXCTN 3 11,072 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  print (output) taaperaban 3 1,920 Sep-03-2021, 04:23 PM
Last Post: deanhystad
  print function output wrong with strings. mposwal 5 3,107 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Print output not working xninhox 7 4,034 Jan-16-2021, 09:42 AM
Last Post: xninhox

Forum Jump:

User Panel Messages

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