[Tutor] how to invert tuples, one problem more

Alan Gauld alan.gauld at btinternet.com
Fri Nov 27 20:42:20 EST 2015


On 27/11/15 20:50, marcus lütolf wrote:

>>>> x = Marcus

Here you assign the variable x to the same thing
as the variable Marcus. But that should be an error
since Marcus is not defined. So I'm guessing you
actually wrote:

>>> x = 'Marcus'

[ It's always better to copy real code into messages
  rather than retype it. ]

>>>> print '{0} {1} x'.format('a', '=')

Here you define a string '{0} {1} x'

The {} sections are place holders, all other characters
are just that - characters to be printed. The x is no
different to the spaces.

Then you apply the format method with the arguments 'a'
and '=' which are just literal characters that get substituted
for their corresponding {} markers in the original string.
Again nothing here refers to variable x or to the string
'Marcus'.

If you want the value of variable x to be printed you
need another format marker. However, since the first
two format markers are being replaced by literal characters
they are pointless so what you really wanted was:

>>> print 'a = {0}'.format(x)

Notice that the x has no quotes around it so is a variable
name that refers to the value 'Marcus' (Assuming my guess
about line 1 was correct!)

You need to clarify in your head the difference between strings
(surrounded by quotes) and variable names (with no quotes and
assigned values).

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list