1

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.

2
  • I think its a little unclear exactly what you want, and what your question is? Do you have an example of what output you exepct/wish for? Commented May 23, 2020 at 8:53
  • @KarlWilhelm Yeah, I think I have made some unsuitable example for this question. I am editing the examples. Sorry! Commented May 23, 2020 at 8:55

4 Answers 4

2

Using a simple capture

exception_string = ""
try:
    a = [10, 20, 30, 40]
    a.push
except Exception as e:
    exception_string = e

print (exception_string)
# 'list' object has no attribute 'push'
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I get what you are saying! I think I would be able to kind of use exception strings using your method. Thanks again!
1

You should use subprocess module. It will store the terminal output in a list. For example:

>>> import subprocess
# enter the command. For example I used "echo arg1 arg2" You can type your .py file directory...
>>> cmd = [ 'echo', 'arg1', 'arg2' ]
>>> output = subprocess.Popen( cmd, stdout=subprocess.PIPE ).communicate()[0]
>>> print (output)
arg1 arg2

Hope it helps 😊...

2 Comments

Thanks, I will try to make it as another file and run with subprocess! Think that would capture the output results!
Yes it will @Gooday2die. Subprocess is only for this purpose only! Please mark it as accepted or do let me know if it worked or not...
1

I'm not sure if I understand your question, but you could simply add the result to an initial string, and call the print method only once:

hello_world_string=''
for i in range(10):
    hello_world_string += "Hello World! " + str(i) + "\n"
print(hello_world_string)

1 Comment

Sorry, I have made you misunderstand the question. I have edited the question a little bit. Thanks!
0

Here is the solution:

hello_world_string=''

for i in range(10): hello_world_string += "Hello World! " + str(i) + "\n" print(hello_world_string)

Output:

Hello World! 0
Hello World! 1
Hello World! 2
Hello World! 3
Hello World! 4
Hello World! 5
Hello World! 6
Hello World! 7
Hello World! 8
Hello World! 9

1 Comment

Sorry! I made some mistakes in the question that I have asked. I have edited the question a bit. Thanks for your answer by the way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.