[Tutor] Closing triple quotation marks.

Alan Gauld alan.gauld at btinternet.com
Sat Jun 18 16:58:23 CEST 2011


"Lisi" <lisi.reisz at gmail.com> wrote

> But I still can't write to the file.
>
> If I do:
> target.write(line1)
>
> The value of the variable line1 is written to the file.

That will work provided line1 is a string.

>  But if I put the three variables into the write command,
> what gets printed is the name of the variables, not their values.

> At the moment it is:
> target.write("""
> line1
> line2
> line3
> """)

This is not three variables it is a single string value that
coincidentally has the names of three of your variables
inside it. But Python has no way to guess that.

You need to be clear on the different between a variable:
a name that references a value(or object if you prefer)
and a string which is a type of value(a string object)

In the first case you pass a name (line1) in the second
you pass a value (the string literal)

> I am beginning to feel paranoid!  I simply can't see in what way 
> that differs
> from yours.

quote signs

> I have, I hope, finally _fully_ taken in that many commands need ().

Actually commands do not usually need (), it is functions and methods
that need (). They are different.

print is a command and it does not need ()  - in Python 2 at least!

print 1,2,"some words"

But file.write() is a method of the file object and must have
parentheses.

f.write("some words")

The () are an instruction to execute the function. Without
the () Python treats the function as an object in its own right:

list_of_objects = [1,"a string",f.write]

for obj in list_of_objects:
    print "Object is: ", obj

> it is possible to get a new line just by giving a new line - without 
> the
> explicit instruction.

That is the unique selling point of triple quoted strings,
you can embed newlines inside them. This makes them
most useful for docstrings (which decribe what a function
is for):

def myFunc():
    """"myFunc()  -> None

     Does some useful background processing with no obvious impact on 
data.
    """
    theAnswer = 7*6


HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/





More information about the Tutor mailing list