title: python3 print to string
body: <p>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:</p>
  
  <p><code>print(f1(), f2(), f3(), sep=getsep(), end=getend())</code></p>
  
  <p>I would like to convert these calls into something like:</p>
  
  <p><code>GuiPrintLine(f1(), f2(), f3(), sep=getsep(), end=getend())</code></p>
  
  <p>where each line is eventually rendered using some (undefined) GUI framework.</p>
  
  <p>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:</p>
  
  <pre><code>s = print_to_string(*args, **kwargs)
  </code></pre>
  
  <p>How do I format a set of parameters to print(...) into a single string that produces the same output as print() would produce?</p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>It seems like this should be trivial in Python, so maybe I'm just missing something obvious.</p>
  
  <p>Thanks for any help!</p>