Print Console Output to a File in Python

refer the code snippet below, it is producing a set of output which is currently displayed either in the output cell or in the terminal, based on the IDE.

But i want to store the whole output to a physical file, for use in future, but not sure how to do that.

for key, value in enumerate(['String1', 'String2']):
    print(key, value)

d = {"key1": "val1", "key2": "val2"}
for i, j in d.items():
    print(i, j)

questions = ['name', 'colour', 'shape']
answers = ['Peter', 'blue', 'a square']
for question, answer in zip(questions, answers):
    print('What is your {0}? I am {1}.'.format(question, answer))

Any help will be appreciable.