Gooday2die
Gooday2die

Reputation: 109

Python Save result as string

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.

Upvotes: 1

Views: 1466

Answers (4)

Abhay Salvi
Abhay Salvi

Reputation: 1099

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 😊...

Upvotes: 1

sandes
sandes

Reputation: 2267

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'

Upvotes: 2

Hamim Yasin
Hamim Yasin

Reputation: 19

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

Upvotes: 0

CanciuCostin
CanciuCostin

Reputation: 1908

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)

Upvotes: 1

Related Questions