Writing a "table" from Python3

How to write both several lines and several variable outputs in one line in a file for output? I know that write () takes only one argument, although ideally this is what I'm trying to achieve:

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

The result is a table.

You can help?

+4
source share
2 answers
 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

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

All Articles