Hrvoje T
Hrvoje T

Reputation: 3913

In Python, how to format string while saving in a file

Is it possible to format strings while saving in a file, similar when printing to a console?

f = open('test.txt', 'w')
f.write('{0:10} {1:10}').format('one', 'two')
f.close()

Upvotes: 0

Views: 795

Answers (1)

lgautier
lgautier

Reputation: 11545

Yes it is. I think that one of your closing parenthesis was not where you meant it to be:

with open('test.txt', 'w') as f:
    f.write('{0:10} {1:10}'.format('one', 'two'))

Upvotes: 1

Related Questions