[Tutor] simple (fnord) newbie question

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Fri, 8 Sep 2000 16:38:27 -0700 (PDT)


On Fri, 8 Sep 2000, R. A. wrote:

> As the following code illustrates, I'm just learning the absolute basics 
> at this point, and I'm trying to pin down a minor detail.  The function 
> performs a couple of simple math formulas, but my question is about the 
> period standing by itself in quotes at the end of a couple of printed 
> statements.

Ah!  Ok, what's happening is when you using the 'print' statement, the
comma adds a space between elements.


> 		print "Variable 'a' now equals", a, "."


Usually, this does the right thing, but if you don't want that space,
you'll need to do something different.  One way to fix this is to
"concatenate" your 'a' with the period.  Strings can be concatenated with
"+".  The obvious way to write this doesn't quite work:

###
>>> a = 10     # just for this example
>>> print "Variable 'a' now equals " + a + "."
Traceback (innermost last):
  File "<stdin>", line 1, in ?
TypeError: illegal argument type for built-in operation
###


The reason it's complaining is because string concatenation has to
concatenate only strings --- Strings do not consort with other things.  
To get this to work, we get the value of 'a' as a string:

###
>>> print "Variable 'a' now equals " + str(a) + "."
Variable 'a' now equals 10.
###


I hope this helps!