converting types

Donn Cave donn at u.washington.edu
Mon May 13 12:44:30 EDT 2002


Quoth Alex Martelli <aleax at aleax.it>:
| ian wrote:
|> hi i know its a pain answering newbie questions
|> that to you lot seem really simple
|> 
|> but how do i convert an int into a string?
|
| Simplest is str(number) -- there are other ways.  As others
| have suggested to you, the % operator, also known as "string
| formatting operator", is generally handiest for most kinds
| of string preparation tasks --
|
| msg = "List is %s long" % number

To me, that's a string representation from an int, more than
a conversion.  This converts an int into a string:

  struct.pack('i', number)

Now I'll concede that based on the frequency with which these
two respective notions are useful, the former is more likely
to be what he had in mind, but then it's also more likely to
be found in any documentation or source example.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list