a Variable in multiple quotes...

John Machin sjmachin at lexicon.net
Sun Feb 13 07:04:08 EST 2005


administrata wrote:
> Is it possible?
>
> I tried...
>
>
> I = "John"
> print \
> """
> I used to love pizza"""
>
>
> Error occurs!!!
>
> But, I don't know how to fix... HELP
>
> thx 4 reading.

Point 0: It helps if you post a copy of what the actual screen display
looked like, instead of just saying "Error occurs!!!".

It helps in that people are more likely to be bothered helping you,
instead of just writing you off as clueless and unclueable -- and as
you have chosen to try Python, you can't be utterly beyond redemption
:-)

Point 1: So, which error? Let's guess:

You got something like this:

!    print \
!           ^
!SyntaxError: invalid token

If so, that is very likely because you had one or more spaces after the
\. Backslash means line-continuation only if it is the LAST character
in the line.

If not, see point 0.

Point 2: so-called multiple quotes: this is nothing to do with your
problem. The different ways of quoting differ only in what characters
like other quotes and newlines can appear inside the quotes.

>>> 'I am John' is """I am John"""
True

Point 3: To print using what you call variables:

>>> I = 'John'
>>> print I, 'used to ...'
John used to ...
>>> print '%s used to ...' % I
John used to ...
>>> I = 'Fred'
>>> print '%s used to ...' % I
Fred used to ...
>>> print I, 'used to ...'
Fred used to ...
>>>

Point 4: How far through the Python tutorial have you progressed?

Point 5: If some bright spark suggests something like the following,
ignore them until you are up to speed on points[:5]:

>>> print '%(I)s used to ...' % locals()
Fred used to ...

Point 6: If you don't understand the meaning of [:5], refer to point 4.

HTH,

John




More information about the Python-list mailing list