Michael F
Michael F

Reputation: 461

Writing a "table" from Python3

How does one write both multiple strings and multiple variable outputs on one line in a file to output? I know that write() only accepts one argument, though ideally this is what I'm trying to achieve:

write('Temperature is', X , 'and pressure is', Y)

The result would be a table.

Can you help?

Upvotes: 4

Views: 1280

Answers (2)

cababunga
cababunga

Reputation: 3114

f = open("somefile.txt", "w")
print('Temperature is', X , 'and pressure is', Y, file=f)

Upvotes: 2

mechanical_meat
mechanical_meat

Reputation: 169324

write('Temperature is {0} and pressure is {1})'.format(X,Y))

If you want more control over the output you can do something like this:

X = 12.3
Y = 1.23
write('Temperature is {0:.1f} and pressure is {1:.2f})'.format(X,Y))
# writes 'Temperature is 12.3 and pressure is 1.2'

Documentation and examples here: http://docs.python.org/py3k/library/string.html

Upvotes: 2

Related Questions