print and softspace in python

P

Phoe6

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 cabc

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()
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.
 
S

Stargaming

Phoe6 said:
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

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."""
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
 
B

Bruno Desthuilliers

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
 

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,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top