Converting itegers to strings

Skip Montanaro skip at mojam.com
Fri Jul 23 19:32:40 EDT 1999


>>>>> "Chris" == Chris Frost <chris at 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 at mojam.com  | http://www.musi-cal.com/~skip/
847-475-3758




More information about the Python-list mailing list