If you really need a print
statement in the list of strings (as opposed to a print-like function with a different name, as suggested in another answer), you can reassign the name print
to your own function, after carefully, carefully, carefully saving the old print function so you can carefully, carefully, carefully restore the name print
to its proper definition. Like this:
>>> g_list = ["print('Wow!')\n", "print('Great!')\n", "print('Epic!')\n"]
>>> old_print = print
>>> def print(s): # redefining the built-in print function! terrible idea
... global catstr
... catstr += s
...
>>> catstr = ""
>>> for s in g_list: exec(s)
...
>>> catstr
'Wow!Great!Epic!'
>>> print = old_print # Don't forget this step!
This is completely immoral, and I did not advise you to do it. I only said you can do it.
To stress the inadvisability of this plan: exec
should be used rarely; it is dangerous; reassigning the names of built-in functions to different functions should be done very rarely; it is dangerous. Doing both in the same code could really ruin your day, especially after a maintenance programmer edits your code "just a little," without realizing the potential impact.