25

Using Python 3, I have a console application that I am porting to a GUI. The code has a bunch of print statements, something like this:

print(f1(), f2(), f3(), sep=getsep(), end=getend())

I would like to convert these calls into something like:

GuiPrintLine(f1(), f2(), f3(), sep=getsep(), end=getend())

where each line is eventually rendered using some (undefined) GUI framework.

This is easy to do if I can convert the arguments to print into to the string that print would normally produce without the side-effect of actually printing to sysout. In other words, I need a function like this:

s = print_to_string(*args, **kwargs)

How do I format a set of parameters to print(...) into a single string that produces the same output as print() would produce?

I realize I could emulate print by concatenating all the args with sep and ends, but I would prefer to use a built-in solution if there is one.

Using print and redirecting sysout is not attractive since it requires modifying the global state of the app and sysout might be used for other diagnostics at the same time.

It seems like this should be trivial in Python, so maybe I'm just missing something obvious.

Thanks for any help!

Brad
  • 3,190
  • 1
  • 22
  • 36
  • 2
    I'm not entirely sure I understand 100%, but does `''.join(args)` achieve what you need? – DaveBensonPhillips Oct 03 '16 at 01:11
  • Sort of, but I don't want to assume anything about the contents or existence of sep/end. I've been thinking something like this might work: ```kwargs['sep'].join(*args, kwargs[end])```, but it is complicated by dealing with the possible absence of sep/end. – Brad Oct 03 '16 at 01:15
  • What would be the difference between `print` and `GuiPrintLine`? – baranskistad Oct 03 '16 at 01:17
  • 2
    `sep.join(args) + end` does seem like the best solution. – Rockybilly Oct 03 '16 at 01:17
  • @bjskistad - GuiPrintLine doesn't actually output the text to sysout. It renders it graphically in some (undefined) way - using tkinter, pygame, whatever... – Brad Oct 03 '16 at 01:18
  • It's kinda hard for the answerer to answer the question if he/she doesn't know exactly what you want `GuiPrintLine` to do. Do you want it to create a curses application? Create a HTTP server and print HTML? – baranskistad Oct 03 '16 at 01:21
  • @bjskistad - I do not need help with the final output of the string. I already know how to handle that part. My question is entirely about getting a series of arguments that would normally go to print formatted into a single string. – Brad Oct 03 '16 at 01:23
  • So you want them formatted as separate strings? – baranskistad Oct 03 '16 at 01:24
  • 1
    I guess that's what's unclear, what are you trying to get done here if you say you don't actually want the output that print would produce? – pvg Oct 03 '16 at 01:28
  • @pvg - I want exactly the output that print would produce, *but as a string*, not as output on stdout. – Brad Oct 03 '16 at 01:36
  • Ah i see. StringIO is what you want. – pvg Oct 03 '16 at 01:49

2 Answers2

38

Found the answer via string io. With this I don't have to emulate Print's handling of sep/end or even check for existence.

import io

def print_to_string(*args, **kwargs):
    output = io.StringIO()
    print(*args, file=output, **kwargs)
    contents = output.getvalue()
    output.close()
    return contents
Brad
  • 3,190
  • 1
  • 22
  • 36
0

My solution :

def print_to_string(*args, **kwargs):
    newstr = ""
    for a in args:
        newstr+=str(a)+' '
    return newstr
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 30 '22 at 09:03