3

I used Learnpythonthehardway.org, and in exercise 16 the study drills say the following: "Use strings, formats, and escapes o print out line 1, line2, and line3 with just one target.write() command instead of six."

And so, I changed:

target.write(line1, "\n", line2, "\n" line3, "\n")
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

Into:

target.write("%s \n  %s \n %s \n") %(line1, line2, line3) 

Can anyone explain what I did wrong?

georg
  • 211,518
  • 52
  • 313
  • 390
Kachachan
  • 33
  • 3

3 Answers3

3

file.write() takes only one argument, not more. Only the print() function handles more than one argument for you.

You need to format your string to one argument instead of trying to 'format' the return value of the target.write() function:

target.write("%s\n%s\n%s\n" % (line1, line2, line3))

or use the print() function to add the newlines and the separators:

print(line1, line2, line3, sep='\n', file=target)

print() as function is the default in Python 3; if you are using Python 2 you can turn print the statement into print() the function by adding:

from __future__ import print_function

at the start of your module.

Note that the original excercise code writes the lines to the file without spaces in between.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • So, the file = target means the line will go into the file named target, correct? If so, would this code show the printed line in the terminal, like how the other print command do. Also, am I not suppose to be saying Thank you? Really new here and the help said so. – Kachachan Nov 19 '13 at 12:11
  • @user2999145: no, the code will not show the lines in the terminal. The default `file` argument is `sys.stdout`, basically the terminal. By specifying `file=target` you tell `print()` to 'print' (write) the data elsewhere. – Martijn Pieters Nov 19 '13 at 12:12
  • @user2999145: We try to keep questions to the point and usable for future visitors. I appreciate 'thank you' as much as the next person, but that is best left to comments, not the question itself. – Martijn Pieters Nov 19 '13 at 12:13
  • No, I meant in the comment section, it said to not say 'Thank you'. – Kachachan Nov 19 '13 at 12:16
  • Yes, saying *just* thank you does clutter up the comments too. We have voting and marking an answer as accepted for those kinds of 'thank you, this helped' responses. :-) – Martijn Pieters Nov 19 '13 at 12:18
  • How do i do that? Really sorry here, but can't seem to find a button for it. Edit: The marking answer as accepted, i meant. – Kachachan Nov 19 '13 at 12:20
3

You have to do like this

target.write("%s \n  %s \n %s \n" % (line1, line2, line3))

Better use string's format method.

target.write("{} \n  {} \n {} \n".format(line1, line2, line3))

Edit:

You might want to look at this question to know why format is better than % formatting

Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • @Levon We already have a question for that http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format – thefourtheye Nov 19 '13 at 12:24
  • Yes, I know that, this is for the benefit of the OP and to make your answer a bit more complete. If you are stating something is better than something else, it seems reasonable to explain why you make that claim (not that I'm disagreeing with it, but given OP's question, they might benefit from a more complete answer) – Levon Nov 19 '13 at 12:27
  • @Levon Can I just include the link that question? – thefourtheye Nov 19 '13 at 12:47
  • Sure,that would serve the same purpose (and give OP a way to find out why this is a better way :) .. that way anyone who sees this answer has all the info they need without having so search for reasons. – Levon Nov 19 '13 at 12:53
  • 1
    @Levon Cool. I just added reference to that question :) – thefourtheye Nov 19 '13 at 12:54
0

Apply the % operator on the string.

target.write("%s \n  %s \n %s \n" % (line1, line2, line3))

Note: for more complex string formatting, use the format method on strings.

sebastian_oe
  • 7,186
  • 2
  • 18
  • 20