Python Forum
How can I get print function output ?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I get print function output ?
#1
i want to save print function output to my var without printing console. (sprintf function in C)

var = specific_print("hello", "my dear", "how are", "you", sep = " ", end = " ?", return_string = True, print_console = False)
print(var)

# output => hello my dear how are you ?
Reply
#2
You can't - print function returns None
In [5]: print(print(1))
1
None
Here is what print function can do
In [7]: print?
Docstring:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
Type:      builtin_function_or_method
There is also no need to. If you want formatted output, you may use formatting options; if you want to concatenate strings with spaces -
' '.join([<list of strings>])
is your friend
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
@volcano63,

Thanks.

i solved my problem with the following code

def myprint(*args, sep=" ",  end="\n"):
    return sep.join(args) + end
Reply
#4
You could also redirect the output to a stringio object...

>>> import io
>>> text = io.StringIO()
>>> print("just some stuff", file=text)
>>> print("more things", file=text)
>>> text.seek(0)
0
>>> text.readlines()
['just some stuff\n', 'more things\n']
>>> text.getvalue()
'just some stuff\nmore things\n'
Reply
#5
+1 for io.StringIO

Or you can rewrite the print function


print_bak = print

def print(*args, **kwargs):
    return args

text = print([1,2,3,4])

print = print_bak
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(May-08-2017, 09:49 PM)nilamo Wrote: You could also redirect the output to a stringio object...

(May-09-2017, 09:39 AM)wavic Wrote: Or you can rewrite the print function

With all due respect, those are more of hacks...
Though for concatenating many small strings into a bigger one io.StringIO is a good option (especially if performance is an issue ..., especially for OP Tongue )
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 318 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  problem in output of a function akbarza 9 1,231 Sep-29-2023, 11:13 AM
Last Post: snippsat
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,123 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to print variables in function? samuelbachorik 3 925 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  How to output one value per request of the CSV and print it in another func? Student44 3 1,360 Nov-11-2022, 10:45 PM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,334 Sep-08-2022, 01:44 PM
Last Post: deanhystad
Sad Want to Save Print output in csv file Rasedul 5 11,032 Jan-11-2022, 07:04 PM
Last Post: snippsat
  Why does absence of print command outputs quotes in function? Mark17 2 1,406 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  return vs. print in nested function example Mark17 4 1,778 Jan-04-2022, 06:02 PM
Last Post: jefsummers
Photo print output none 3lnyn0 4 1,862 Nov-01-2021, 08:46 PM
Last Post: 3lnyn0

Forum Jump:

User Panel Messages

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