how to convert string
Ben C
spamspam at spam.eggs
Wed Apr 5 14:45:13 EDT 2006
On 2006-04-05, diffuser78 at gmail.com <diffuser78 at gmail.com> wrote:
> I want to print number 0 to 9 in one line like this
> 0 1 2 3 4 5 6 7 8 9
>
> if I do like this, it prints in different lines
>
> for i in xrange(10):
> print i
for i in xrange(10):
print i,
should work (comma after the i).
> so i tried like this
>
> str = ""
> for i in xrange(10):
> str = i + " "
> print str
>
> but i want to know how convert int i to string.
There's a builtin function str (better not to call your string str).
Here I've called it s:
s = ""
for i in xrange(10):
s = str(i) + " "
print s
But this puts an extra space on the end (so did the print i, version
above). Might be better therefore to use string.join:
import string
s = string.join(map(str, xrange(10)), " ")
print s
More information about the Python-list
mailing list