[Tutor] Are there other ways of solving this exercise?

Walter Prins wprins at gmail.com
Thu Jan 12 21:52:33 CET 2012


On 12 January 2012 16:57, amt <0101amt at gmail.com> wrote:
> I'll give it another try:

> So the code should look like this:
>
> bag = "{0}\n{1}\n{2}".format(line1,line2,line3)
> target.write(bag)
>
Yes.

>> Final comment, you can get rid of the variable "bag" by directly
>> printing the result of the call to format() like you did in your
>> previous solution.

> You mean print "{0}\n{1}\n{2}\n".format(line1,line2,line3)?

No, print as such is actually besides the point, it was just used to
actually output the string with some interpretation given to the
newlines.


> Ok, but if I drop the variable bag and print directly,how will I write
> line1,line2,line3 in the .txt file since I have no parameter to give
> to the write method.(target.write() ) ?

Well in fact you do, in the same way that you have one in your
original solution which had no intermediate "bag" variable.

OK let's backtrack a bit and try to clarify.  In your original solution you had:

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

What's happening here?  Firstly you're calculating a string expression, namely:

"%s\n%s\n%s\n" %(line1, line2, line3)

Let's try this in the Python interpreter:

Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit
(AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> "%s\n%s\n%s\n" % ('aaa','bbb','ccc')
'aaa\nbbb\nccc\n'
>>> print "%s\n%s\n%s\n" % ('aaa','bbb','ccc')
aaa
bbb
ccc

>>>

Notice, first I type the expression directly at the interpreter
prompt.  Python therefore evaluates the epxression types, and as a
courtesy displays the result, then discards it.  On the next line, I
repeat the excercise, but this time prepend a print statement to it.
This is essentially giving the result of the expression evaluation to
the print statement.  Now the job of the print statement is also to
display the value of variables, but it applies a bit more
interpretation to what it's given, and so it actually interpretets the
"newline" characters in the result string, displayed as "\n" in the
string on the previous line, and so the literal "aaa", "bbb" and "ccc"
ends up on seperate lines.

Now consider your original line again:
target.write("%s\n%s\n%s\n" %(line1, line2, line3))

What's actually happening here?  Well, as happens in the interactive
interpreter example above, firstly the string expression using the %
operator is evaluated, which results in a string result as above, but
using the contents of line1, line2 and line3.  This result is then
passed as the parameter to the target.write() method.

Now, the exact same thing happens when you use a string method.  The
string method str.format() also returns a result, namely the string
that results when formatting the format string with the parameter
values specified.  So in this sense it's no different to what you had
before.  Here's an interactive Python demonstration again:

Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit
(AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> '{0}\n{1}\n{2}\n'.format("aaa","bbb","ccc")
'aaa\nbbb\nccc\n'
>>> print '{0}\n{1}\n{2}\n'.format("aaa","bbb","ccc")
aaa
bbb
ccc

>>>

Notice, the mechanics is exactly the same.  Consequently, you can
write your code without the intermediate "bag" variable, just like you
didn't need it in your original solution:
target.write( '{0}\n{1}\n{2}\n'.format(line1,line2,line3) )

To belabor the point: here the call to str.format() returns a result
(another string) which is immediately and directly passed to
target.write() for writing to the file.

> Walter, thanks a lot for taking your time to help me out.

You're welcome, hope it helps.

Walter


More information about the Tutor mailing list