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. 13 2275 di********@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,
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 di********@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
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> di********@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
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
Every help is appreciate.
On 2006-04-05, di********@gmail.com <di********@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
Ben C wrote: ... 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'
--
-Scott David Daniels sc***********@acm.org di********@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
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
On 2006-04-05, Scott David Daniels <sc***********@acm.org> wrote: Ben C wrote: ... 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...
for i in xrange(10):
print i,
this could be fine.
Try this
for x in range(10):
sys.stdout.write(x)
sys.stdout.write(" ")
0 1 2 3 4 5 6 7 8 9 di********@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
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.
a couple more exotic variations
print (10 * "%s ") % tuple(range(10))
print filter(lambda x : x not in "[,]",str(range(10)))
Alle 08:51, giovedě 06 aprile 2006, sw******@yahoo.com 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
"Fulvio" <fu****@pc.jaring.my> wrote: 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>
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Joel Moore |
last post by:
Maybe I'm just easily baffled after an all-nighter but I can't seem to
figure out how to represent a BitArray as a hexadecimal string.
For example:
Dim outputBank As New BitArray(8)
...
|
by: Eric Lilja |
last post by:
Hello, I've made a templated class Option (a child of the abstract base
class OptionBase) that stores an option name (in the form someoption=) and
the value belonging to that option. The value is...
|
by: aevans1108 |
last post by:
expanding this message to microsoft.public.dotnet.xml
Greetings
Please direct me to the right group if this is an inappropriate place
to post this question. Thanks.
I want to format a...
|
by: Convert TextBox.Text to Int32 Problem |
last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
|
by: patang |
last post by:
I want to convert amount to words. Is there any funciton available?
Example:
$230.30
Two Hundred Thirty Dollars and 30/100
|
by: patang |
last post by:
Could someone please tell me where am I supposed to put this code. Actually
my project has two forms. I created a new module and have put the following
code sent by someone. All the function...
|
by: GM |
last post by:
Dear all,
Could you all give me some guide on how to convert my big5 string to
unicode using python? I already knew that I might use cjkcodecs or
python 2.4 but I still don't have idea on what...
|
by: tshad |
last post by:
I am trying to convert a string character to an int where the string is all
numbers.
I tried:
int test;
string stemp = "5";
test = Convert.ToInt32(stemp);
But test is equal to 53.
|
by: Marco Nef |
last post by:
Hi there
I'm looking for a template class that converts the template argument to a
string, so something like the following should work:
Convert<float>::Get() == "float";
Convert<3>::Get() ==...
|
by: Debadatta Mishra |
last post by:
Introduction
In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |