title: Python Save result as string
body: <p>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.</p>
  
  <p>What I want is like it is below.</p>
  
  <pre><code>for i in range(10):
      print("Hello World! " + str(i))
  </code></pre>
  
  <p>For example, I have this kind of code here. The result of this expression is kind of obvious.</p>
  
  <pre><code>Hello World! 0
  Hello World! 1
  ...
  Hello World! 8
  Hello World! 9
  </code></pre>
  
  <p>I would like to store this whole result as inside a string. Such as <code>hello_world_string</code>.
  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.</p>
  
  <p>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.</p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>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.</p>
  
  <p>For example,</p>
  
  <pre><code>a = [10, 20, 30, 40]
  a.push
  </code></pre>
  
  <p>We have some error here at the example up.
  The reuslt of this code would be</p>
  
  <pre><code>AttributeError: 'list' object has no attribute 'push'
  </code></pre>
  
  <p>I would like to save this output as a string.</p>
  
  <p>The question seems to be misleading. I am deeply sorry for that.</p>
  
  <p><strong>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</strong>.</p>
  
  <p>Again, I am deeply sorry for making everyone confused. 
  Thanks.</p>