467,077 Members | 949 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

print and softspace in python

print and softspace in python
In python, whenever you use >>>print statement it will append a
newline by default. If you don't want newline to be appended, you got
use a comma at the end (>>>print 10,)
When, you have a list of characters and want them to be printed
together a string using a for loop, there was observation that no
matter what there was space coming between the characters. No split
or join methods helped.
>>>list1=['a','b','c']
for e in list1:
print e,
a b c
>>># Without whitespace it will look like.
print "abc"
abc

The language reference says that print is designed to output a space
before any object. And some search goes to find and that is controlled
by softspace attribute of sys.stdout.
Way to print without leading space is using sys.stdout.write()
>>>import sys
for e in list1:
sys.stdout.write(e)
abc

Reference manual says:
A space is written before each object is (converted and) written,
unless the output system believes it is positioned at the beginning of
a line. This is the case (1) when no characters have yet been written
to standard output, (2) when the last character written to standard
output is "\n", or (3) when the last write operation on standard
output was not a print statement. (In some cases it may be functional
to write an empty string to standard output for this reason.)

Question to c.l.p
Am Not getting the last part as how you will write a empty string and
use print not appending blank space in a single line. Am not getting
the (In some cases... ) part of the reference manual section. Please
help.

Mar 14 '07 #1
  • viewed: 10083
Share:
2 Replies
Phoe6 schrieb:
print and softspace in python
In python, whenever you use >>>print statement it will append a
newline by default. If you don't want newline to be appended, you got
use a comma at the end (>>>print 10,)
When, you have a list of characters and want them to be printed
together a string using a for loop, there was observation that no
matter what there was space coming between the characters. No split
or join methods helped.
Huh?
>>x = ['h', 'i', '!']
print ''.join(x)
hi!

I don't see any problem there. In the most cases you could also build up
a string accumulatedly.

>>>>list1=['a','b','c']
for e in list1:

print e,
a b c
>>>># Without whitespace it will look like.
print "abc"

abc

The language reference says that print is designed to output a space
before any object. And some search goes to find and that is controlled
by softspace attribute of sys.stdout.
Way to print without leading space is using sys.stdout.write()
"Note: This attribute is not used to control the print statement, but to
allow the implementation of print to keep track of its internal state."""
>
>>>>import sys
for e in list1:

sys.stdout.write(e)
abc

Reference manual says:
A space is written before each object is (converted and) written,
unless the output system believes it is positioned at the beginning of
a line. This is the case (1) when no characters have yet been written
to standard output, (2) when the last character written to standard
output is "\n", or (3) when the last write operation on standard
output was not a print statement. (In some cases it may be functional
to write an empty string to standard output for this reason.)

Question to c.l.p
Am Not getting the last part as how you will write a empty string and
use print not appending blank space in a single line.
I'd guess they think about print "",;print "moo" (print a blank string,
do not skip line, print another one) to preserve the "softspace". As far
as I get your problem, you don't really have to think about it.
Am not getting
the (In some cases... ) part of the reference manual section. Please
help.
Use the join-idiom correctly.

HTH,
Stargaming
Mar 14 '07 #2
Phoe6 a écrit :
print and softspace in python
In python, whenever you use >>>print statement
Drop the '>>>' part. It's just the default Python shell prompt.
it will append a
newline by default. If you don't want newline to be appended, you got
use a comma at the end (>>>print 10,)
When, you have a list of characters and want them to be printed
together a string using a for loop,
Why would one use a for loop to do so ? This is inefficient (even in C -
it's mostly a system limitation). If you have a list of characters
(actually, a list of one-character strings - there's no 'char' type in
Python) and want to print it as a string, then first turn that list into
a string, then print it:
>>list1=['a','b','c']
print "".join(list1)
HTH
Mar 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bertram Scharpf | last post: by
30 posts views Thread by Martin Bless | last post: by
9 posts views Thread by Paul Watson | last post: by
reply views Thread by David Bolen | last post: by
14 posts views Thread by Marcin Ciura | last post: by
2 posts views Thread by jamesthiele.usenet@gmail.com | last post: by
11 posts views Thread by A.M | last post: by
10 posts views Thread by Prisoner at War | last post: by
7 posts views Thread by bukzor | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.