The last, most structured alternative for generating code is to construct the tree data structure representing the syntax, and then transforming this back into source code form.
This approach to code generation is perhaps the most applicable to the programmatic generation of code, and less the generation directly by a user.
from cgen import *
func = FunctionBody(
FunctionDeclaration(Const(Pointer(Value("char", "greet"))), []),
Block([Statement('return "hello world"')])
)
print(func)