Mailing List Archive

Converting itegers to strings
--Nq2Wo0NMKNjxTN9z
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

How can you convert numbers (an interger in this case) to strings? (i.e. I
have some_number =3D 3, and would like to use it as a string)

thanks,
--=20
Chris Frost | <http://www.frostnet.advicom.net/chris/>
-------------+------------------------------------------
Public PGP Key:
Email chris@frostnet.advicom.net with the subject "retrieve pgp key"
or visit <http://www.frostnet.advicom.net/chris/about/pgp_key.phtml>

--Nq2Wo0NMKNjxTN9z
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 5.0i for non-commercial use
MessageID: /DrbI5JyO0+ZIkse7pRDgxpReNFHPTmn

iQA/AwUBN5jcWeEzIlbKpewXEQIReQCeJ4Ronj15A2PqQwuzpZNov9jbrQ0AnjOU
vzMD2tU6hFf/zx5F+tP5q+PT
=aXar
-----END PGP SIGNATURE-----

--Nq2Wo0NMKNjxTN9z--
Converting itegers to strings [ In reply to ]
Hi All--

Chris Frost wrote:
>
> How can you convert numbers (an interger in this case) to strings? (i.e. I
> have some_number = 3, and would like to use it as a string)
>

some_number = `3`

;-)

or

some_number = str(3)

You might want to consider joining the python-tutor mailing list. You
can find out how at

http://www.python.org

<python-slices-and-dices-too>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan@callware.com
ivanlan@home.com
http://www.pauahtun.org
See also:
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
----------------------------------------------
Converting itegers to strings [ In reply to ]
>>>>> "Chris" == Chris Frost <chris@frostnet.advicom.net> writes:

Chris> How can you convert numbers (an interger in this case) to
Chris> strings? (i.e. I have some_number = 3, and would like to use it
Chris> as a string)

There are a couple ways. Try the % operator, backquotes or the str builtin
function.

import math
"%d" % 3
type("%d" % 3)
`math.pi`
type(`math.pi`)
("%6.1f" % math.pi)
type("%6.1f" % math.pi)
str(math.pi)
type(str(math.pi))

All should print
Skip Montanaro | http://www.mojam.com/
skip@mojam.com | http://www.musi-cal.com/~skip/
847-475-3758
Converting itegers to strings [ In reply to ]
Chris Frost wrote in message <19990723171921.A5205@pooh.frostnet.net>...

>How can you convert numbers (an interger in this case) to strings? (i.e. I
>have some_number = 3, and would like to use it as a string)


Yeah, there's no itoa() -- use the printf equivalent:
>>> print "%d" % some_number
3

Multiple numbers are supplied as a tuple:
>>> h = 7
>>> m = 45
>>> print "%02d:%02d" % (h,m)
07:45
--
Phil Mayes pmayes AT olivebr DOT com