String adding.

Alex Martelli aleaxit at yahoo.com
Wed Oct 18 03:38:08 EDT 2000


"Stephen Kloder" <stephenk at cc.gatech.edu> wrote in message
news:39ED099F.1B3DB0F8 at cc.gatech.edu...
    [snip]
> > strName = 'wow'
> >  '<' + strName + '>'
> >
> > will give you  <wow>
> >
> > but what about:
> >
> > intName = 3
> > print '<' + intName + '>'
> > this gives me some illegal argument type for built-in operation..
> > how would i make it give me <3>????
>
> >>> intName=3
> >>> print '<' + `intName` + '>'    # (Those are back-ticks, not quotes)
> <3>
>
> BTW `x` is the same as repr(x)

Exactly because of this, I would suggest
    "<%s>" % whatever
as a preferable solution to
    '<' + `whatever` + '>'

The %-solution calls str(whatever), rather than repr(whatever),
and (at least as I understand things) str() is the one that is
supposed to give you a readable-string as opposed to a more
detailed 'string representation'.

Not important (no difference...) when whatever is an integer,
but...:

>>> foo=23L
>>> '<' + `foo` + '>'
'<23L>'
>>> '<%s>' % foo
'<23>'
>>>

I think one is more likely to want the 'cleaner' output of
the second example, here, rather than the explicit L that
repr uses to demark foo as long-integer.


Alex






More information about the Python-list mailing list