Wikipedia:Reference desk/Archives/Computing/2019 November 14

= November 14 =

Convert *args to a single string in Python 3
Let's a say I have a thin wrapper around print in Python 3:

And now instead of printing directly to console, I want to log to a log file instead:

However since input format for print differs from that of the logger's, the multiple parameter logging statement now throws an error.

Is there a way to convert the print *args to a single string so that it can be fed into logger.info? Mũeller (talk) 14:45, 14 November 2019 (UTC)


 * May be this thread at StackOverflow will be of some use for you...?
 * python3 print to string
 * Once you get your output in a strig, pushing it further either to a file, to some logging interface or to GUI is easier. --CiaPan (talk) 15:26, 14 November 2019 (UTC)


 * One way would be ' '.join([str(arg) for arg in args]) (similar to what print does, and much simpler than the linked SO solution to a more complex question).
 * One could argue for ' '.join([repr(arg) for arg in args]) instead if you want to later parse the logs and recreate some objects (see ). I would argue that if you need to log anything else than strings and integers, "dump everything in a text file" is an approach too crude, but YMMV. Tigraan Click here to contact me 15:47, 14 November 2019 (UTC)
 * Perfect! Thank you so much, you two! Much appreciated. Mũeller (talk) 00:51, 15 November 2019 (UTC)

I think you can use:

That is the log function receives all those args as a single tuple, and it passes the tuple to logging.info which handles formatting the tuple. That works in python 3.8 but I didn't check earlier python versions. 67.164.113.165 (talk) 17:24, 15 November 2019 (UTC)
 * That's a very cool feature. I didn't know that before. Though unfortunately in this case it adds extraneous brackets in the output (since it's passed in as a tuple, like you mentioned):


 * Also I slightly prefer " " instead of ", " as the separator, because I got used to writing

Mũeller (talk) 04:38, 16 November 2019 (UTC)

Python 3.8 has that built in: log(f'{variable_x=}'). 67.164.113.165 (talk) 19:08, 16 November 2019 (UTC)
 * Very nice, thanks! Python gets better every year. Mũeller (talk) 04:32, 17 November 2019 (UTC)