Concatening string and integer

Ulf Engstrøm ulf.engstrom at b2b-link.com
Fri Apr 28 05:16:34 EDT 2000


Also important to remember when using ` with strings is this:
>>> text = 'text'
>>> `text`
"'text'"
>>> num = 5
>>> `num`
'5'

Adding an unknown variable to a textstring, the variable being a number is
fine, being a string already gives you
'text "variable" and more text', not very pretty.

(Other way to deal with that is 'text %s and more text' %
(unknownvariabletype), or checking type() before using it)
Regards
Ulf

> Actually the back ticks appear to be a shortcut for repr() not str()
> which in the above case give the same result but won't for all objects,
> for example:
>
> >>> class aclass:
> ...     def __str__(self):
> ...         return 'spam'
> ...
> >>> a = aclass()
> >>> `a`
> '<__main__.aclass instance at f93a8>'
> >>> str(a)
> 'spam'
> >>> repr(a)
> '<__main__.aclass instance at f93a8>'
>
> Cheers,
> Anthony
> _____________________________________________________________________
> Anthony J Wilkinson                               anthony at dstc.edu.au
> Software Engineer                                http://www.fnorb.com
> DSTC Pty Ltd                                     Ph:  +61 7 3365 4310
>
>
>
> --
> http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list