Lisp type languages usually provide an abstraction which allows you to open a string as a file, and then print to it using any function which can normally print to a file. When you close the string you can recuperate the string that was printed.
Does something like this exist for python?
It probably would like something like this:
f = open_string_as_output()
f.write("hello")
if some_condition:
f.write(" ")
g(f) # allow g to write to the sting if desired
f.close()
str = f.get_string()