Ollie
Ollie

Reputation: 11

How can i save this into a variable so that i can save it to a file

I have a bit of code which prints what I want to save but I cant save it as a variable because of the format. Please can you show me how to save this as a variable so that I can save it into a file

It wont let me add a picture but this is what I want to add to a variable (What its printing)

print(text[i],end="")

Upvotes: 1

Views: 61

Answers (2)

Igor
Igor

Reputation: 2909

Open a file:

f = open('filename','w')

Write a line of text to the file:

f.write(text[i])

And finally close the file:

f.close()

Upvotes: 1

Chuck
Chuck

Reputation: 874

x = text[i]
with open("output.txt", 'w') as f:
    f.write(x)

or

with open("output.txt", 'w') as f:
    f.write(text[i])

Upvotes: 2

Related Questions