how to convert string

D

diffuser78

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

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.

Every help is appreciate.
 
L

Leif K-Brooks

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,
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.

s = "" # Don't shadow the str builtin.
for i in xrange(10):
s += str(i) + " "
print s
 
B

Ben C

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
 
F

Fredrik Lundh

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

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.

the conversion function is called str(), so you might
want to rename that variable...

out = ""
for i in xrange(10):
out = out + str(i) + " "
print out

on the other hand, since print adds spaces between items printed
on the same line, you can simply do

for i in range(10):
print i,
print

or you could use join, like

print " ".join(str(i) for i in range(10))

or

print " ".join(map(str, range(10)))

</F>
 
G

Gary Herron

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
A comma at the end of the print will do what you want:
for i in xrange(10):
print 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.
str(i) will do what you want and so will the "%" operator. However, you
sample code masks the builtin "str" function by creating a variable with
the same name (plus you had another error in that line), so try this:

s = ""
for i in xrange(10):
s = s + str(i) + " "
print s

A shortcut for the line in the loop is
s += str(i) + " "

Also note that appending to string is slow while appending to lists is not. So try build a list and turning it into a string with the string "join" method like this
l = []
for i in xrange(10):
l.append(str(i)
print " ".join(l)

Finally, you can build the list with a list comprehension construct
l = [str(i) for i in xrange(10)]
print " ".join(l)

You could also combine the above two lines into one -- that would be shorter, but probably not clearer.

Cheers,
Gary Herron
 
S

Scott David Daniels

Ben said:
... But this puts an extra space on the end (so did the print i,
version above).
Actually, no (the trailing-comma prints do a funny dance).
Check it out:

from StringIO import StringIO
dest = StringIO()
for i in range(10):
print >>dest, i,
print >>dest
print repr(dest.getvalue())

prints:
'0 1 2 3 4 5 6 7 8 9\n'
 
P

Peter Hansen

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

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.

Every help is appreciate.

I think you'd learn the answers to this question and many more that
you're likely to ask if you would do the online tutorial at
http://docs.python.org/tut/tut.html

-Peter
 
B

Ben C

Ben said:
... But this puts an extra space on the end (so did the print i,
version above).
Actually, no (the trailing-comma prints do a funny dance).
Check it out: [...]

You're right, I tried it! Thanks for that.

Useful, although I hope Python doesn't turn into a "do what I mean"
language...
 
S

swami426

Try this

for x in range(10):
sys.stdout.write(x)
sys.stdout.write(" ")


0 1 2 3 4 5 6 7 8 9
 
A

Azolex

a couple more exotic variations

print (10 * "%s ") % tuple(range(10))

print filter(lambda x : x not in "[,]",str(range(10)))
 
F

Fulvio

Alle 08:51, giovedì 06 aprile 2006, (e-mail address removed) ha scritto:
for x in range(10):
        sys.stdout.write(x)
        sys.stdout.write(" ")

BTW, how to write a number repeatly in the same line and position, without let
the printout to scroll down.

F
 
F

Fredrik Lundh

Fulvio said:
BTW, how to write a number repeatly in the same line and position, without let
the printout to scroll down.

for i in range(100):
print "\r", i,
# do something
print

will work, as long as the message isn't too long, and you're printing to a
console device that does "the usual thing" when it sees a carriage return.

</F>
 
F

Fulvio

Alle 18:21, giovedì 06 aprile 2006, Fredrik Lundh ha scritto:
will work, as long as the message isn't too long
I was trying some

print" \b\b\b\b", i,
For a number of 4 digit, but I think I miscalculated some lenght variation.

The reason of this is because it won't leave previous printing. But I also got
some wrong positioning :)

F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,171
Latest member
EllaHolmwo

Latest Threads

Top