how to print without blank?

J

Ju Hui

I want to print 3 numbers without blank..... print x
....
0
1
2.... print x,
....
0 1 2

how to print
012
?

thanks.
 
I

Ivan Herman

Ju said:
I want to print 3 numbers without blank.
... print x
...
0
1
2
... print x,
...
0 1 2

how to print
012
?

thanks.

You may want to use the write command with stdout:

sys.stdout.write("%s" % x)

using 'write' you have much more facilities to format the output...

I hope this helps

Ivan
 
S

Steven D'Aprano

I want to print 3 numbers without blank. [snip]
how to print
012
?

Method one: accumulate your numbers into a single string, then print it in
one go.
.... L.append(str(x))
....

Or even:


Or use a list comprehension:
print ''.join([str(x) for x in range(3)]) 012



Method two: don't use the print statement, but write directly to standard
output.
.... sys.stdout.write(str(x))
....
012>>>


But notice how this does not write a newline when you are done -- you will
have to remember to do it yourself with sys.stdout.write('\n').

Also, print will work with any object, but sys.stdout.write will only work
with strings:
Traceback (most recent call last):
 
R

Rick Zantow

I want to print 3 numbers without blank. [snip]
how to print
012
?

Method one: accumulate your numbers into a single string, then print
it in one go.
L = []
for x in range(3):
... L.append(str(x))
...

Or even:


Or use a list comprehension:
print ''.join([str(x) for x in range(3)]) 012



Method two: don't use the print statement, but write directly to
standard output.
... sys.stdout.write(str(x))
...
012>>>


But notice how this does not write a newline when you are done -- you
will have to remember to do it yourself with sys.stdout.write('\n').

Also, print will work with any object, but sys.stdout.write will only
work with strings:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: argument 1 must be string or read-only character buffer,
not int

Although the sysout.write() approach is (IMHO) the best, for the OP's
case, he could try method three:

for x in range(3):
print '\b%d' % x,
 
J

Ju Hui

thank you all. IT's very helpful to me..... sys.stdout.softspace = 0
.... return x
........ print no_space_before(x),
....
012
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top