Printing dots in single-line

G

Guest

Hey there,

I want to print some dots in a single-line while my program loads or does
something. I tried with he following but it didn't work :(.

while 1:
print '.',

Prints line of dots separated by a whitespace (. . . . . . etc). Is there a
way I can get it to display them without that white space (.......etc)?

Thanks
 
J

Jay O'Connor

Hey there,

I want to print some dots in a single-line while my program loads or does
something. I tried with he following but it didn't work :(.

while 1:
print '.',

Prints line of dots separated by a whitespace (. . . . . . etc). Is there a
way I can get it to display them without that white space (.......etc)?

import sys

while 1:
sys.stdout.write(".")
 
B

Bob Gailer

At said:
Hey there,

I want to print some dots in a single-line while my program loads or does
something. I tried with he following but it didn't work :(.

while 1:
print '.',

Prints line of dots separated by a whitespace (. . . . . . etc). Is there a
way I can get it to display them without that white space (.......etc)?

Try (depending on the output device)
while 1:
print '.\b',

Bob Gailer
(e-mail address removed)
303 442 2625
 
E

Erik Max Francis

Bob said:
Try (depending on the output device)
while 1:
print '.\b',

A _far_ superior solution would be to simply not print the trailing
spaces in the first place:

while True:
sys.stdout.write('.')
sys.stdout.flush()

--
Erik Max Francis && (e-mail address removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \
\__/ Love is like war: easy to begin but very hard to stop.
-- H.L. Mencken
 
D

Duncan Booth

Try (depending on the output device)
while 1:
print '.\b',

Yuck.

Either suppress the whitespace:

import sys
for i in range(10):
sys.stdout.softspace=False
print '.',

or, much simpler, just don't use print:

for i in range(10):
sys.stdout.write('.')

The reason I say that 'write' is simpler is that the softspace attribute
works in a slightly confusing manner. You have to set it false every time
before you print something if you don't want Python putting a leading space
before the string it outputs. It gets sets true after each item that is
printed unless a newline has just been output.
 
A

Alok Singhal

Hey there,

I want to print some dots in a single-line while my program loads or
does something. I tried with he following but it didn't work :(.

while 1:
print '.',

Prints line of dots separated by a whitespace (. . . . . . etc). Is
there a way I can get it to display them without that white space
(.......etc)?

print('.' * n)

where n is the number of dots you want to print.

Incidentally, I saw other responses, and most of them suggest
sys.stdout.write(). Is there a reason that is preferable over the
solution above?

Alok
 
R

Rainer Deyke

Alok said:
Incidentally, I saw other responses, and most of them suggest
sys.stdout.write(). Is there a reason that is preferable over the
solution above?

Yes: the print statement is an abomination. It is a special-case syntax for
a task that requires no special syntax, and will hopefully be deprecated in
the future, the sooner the better.

It is also not as general as sys.stdout.write. It may be important that the
dots are printed in separate statements. For example, the dots could be
used as a progress indicator.
 
J

Jay O'Connor

Alok said:
print('.' * n)

where n is the number of dots you want to print.

Incidentally, I saw other responses, and most of them suggest
sys.stdout.write(). Is there a reason that is preferable over the
solution above?

The solution you give will not work when you want to do processing
between each dot.
 
P

Paul Rubin

Rainer Deyke said:
Yes: the print statement is an abomination. It is a special-case syntax for
a task that requires no special syntax, and will hopefully be deprecated in
the future, the sooner the better.

Are you kidding? The example I keep asking about is the addition
operator (+). Why does anyone need to say 2+2 when they can say
2-(-2)? The addition operator is a special case syntax for the
subtraction operator where none is needed. Should we deprecate it?
I'd rather say that a practical language needs to make concessions to
the way users actually think.
 
J

Jay O'Connor

Paul said:
Are you kidding? The example I keep asking about is the addition
operator (+). Why does anyone need to say 2+2 when they can say
2-(-2)? The addition operator is a special case syntax for the
subtraction operator where none is needed. Should we deprecate it?
I'd rather say that a practical language needs to make concessions to
the way users actually think.


Well, okay then :) Personally, print bothers me because there is no
receiver. As an OO developer, I'm used to thinking usually iin terms of
"object.method" and the fact that print is just sorta this standalone
thing out there with no receiver doesn't match the way I think.
However, it's not really a function either as it doesn't use function
syntax. It's just sorta there. It really doesn't fit in with any of the
basic idioms I use when thinking about developing software
 
M

Michael Hudson

Rainer Deyke said:
Yes: the print statement is an abomination.

Says you.

I mean, just take a look at Joe Strout's brilliant little "python
for beginners" page. Replace all print-statements with
sys.stdout.write( string.join(map(str, args)) + "\n") and you
surely won't get any new beginners. And That Would Be A Very Bad
Thing.
-- Fredrik Lundh, 27 Aug 1996

Cheers,
mwh
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top