I was wondering if I can save my Python execution result as a string. I have tried Eval, Exec, and Compile and those were not the results that I wanted.
What I want is like it is below.
for i in range(10):
print("Hello World! " + str(i))
For example, I have this kind of code here. The result of this expression is kind of obvious.
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.
I am quite a newbie to StackOverflow. So I might have made some errors. If so, please let me know. I will be happy to modify them as soon as possible.
Edit:
Sorry, I kind of made some mistakes in this question. I think the code up is not the right example of what I would like to do.
For example,
a = [10, 20, 30, 40]
a.push
We have some error here at the example up. The reuslt of this code would be
AttributeError: 'list' object has no attribute 'push'
I would like to save this output as a string.
The question seems to be misleading. I am deeply sorry for that.
To be more specific, the Python Interpreter or Jupyter notebook "Prints Out" how codes went and outputs of each process. I would like to capture those "Printed Out" data as a string regardless of the code itself.
Again, I am deeply sorry for making everyone confused. Thanks.