4

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?

2 Answers 2

2
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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

He wants to output to a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.