Python将结果另存为字符串

我想知道是否可以将Python执行结果另存为字符串。我尝试了Eval,Exec和Compile,但这些并不是我想要的结果。

我想要的是下面的样子。

for i in range(10):
    print("Hello World! " + str(i))

例如,我在这里有这种代码。这种表达的结果是显而易见的。

Hello World! 0
Hello World! 1
...
Hello World! 8
Hello World! 9

I would like to store this whole result as inside a string. Such as hello_world_string. I kind of know that saving that first code part as a file and then executing it using Python by subprocess or similar function methods does return string type as their return values. However, I would like to know if there is any way that I can do this without writing into another file.

我是StackOverflow的新手。所以我可能犯了一些错误。 如果是这样,请告诉我。我很乐意尽快对其进行修改。

谢谢。