I'm working my way through Zed Shaw's "Learn Python The Hard Way". I'm up to exercise 16 ( http://learnpythonthehardway.org/book/ex16.html ) and am running into a problem figuring out extra credit # 3. There are a series of 6 target.write commands towards the bottom of the script and Zed wants me to simplify them into a single target.write command using strings, formats and escapes.
Here is the original script with the 6 target.write commands...
from sys import argv
script, filename = argv
print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."
print "Opening the file..."
target = open(filename, 'w')
print "Truncating the file. Goodbye!"
print "Now I'm going to ask you for three lines."
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
print "I'm going to write these to the file."
print "And finally, we close it."
target.write (line1, line2, line3)
target.write "I love %r and %r and %r." % (line1, line2, line3)
target.write (line1), (line2), (line3)
target.write (line1, "\n", line2 "\n", line3, "\n")
You could do
Or, maybe written a little more clearly:
lines=(line1,line2,line3) target.write( '\n'.join(lines) + '\n')
Although, in this case, I might actually write the last newline separately to avoid the overhead of creating an entirely new string just to add on a newline at the end (and I think it looks cleaner):
lines=(line1, line2, line3) target.write('\n'.join(lines)) target.write('\n')
This takes your lines, packs them together as a tuple and uses the
join method of string objects to make them into a single string which gets written.
The advantage to this approach (as opposed to string formatting) is that you don't need to know a-priori how many "lines" you are going to write. Any iterable object will do work in place of the tuple in the above expression.