470,590 Members | 2,650 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,590 developers. It's quick & easy.

converting an integer to a string

I have a quick simple question. How do you convert an integer to a string in


Ken Fettig
Jul 18 '05 #1
2 344192

Ken> I have a quick simple question. How do you convert an integer to a
Ken> string in Python?

Take your pick:

'%d' % someint

Which is most appropriate may well depend on your tastes and your context.
`someint` is just syntactic sugar for repr(someint), and is falling out of
favor with many people. In the case of integers, str(someint) and
repr(someint) are the same, so your choice there is a tossup unless you are
str()'ing or repr()'ing other objects as well (str() generally tries to be
"readable", repr() generally tries to be "parseable"). For most types
repr() and str() generate different output. The experiment is probably
educational enough to perform once, so I won't go into detail.

The %-format version is appropriate if you want to embed it into a larger
string, e.g.:

'%s is %d years old' % (person, age)

Don't forget the dict form as well:

'%(name)s is %(age)d years old' % locals()


Jul 18 '05 #2
The % format also allows you some formatting options that the others

John Roth
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Peter Warder | last post: by
6 posts views Thread by Alex Neumann | last post: by
2 posts views Thread by Dan | last post: by
1 post views Thread by Kevin S Gallagher | last post: by
13 posts views Thread by Pierre Quentel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.