473,699 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.writ e()
>>>import sys
for e in list1:
sys.stdout.writ e(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
2 11017
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.writ e()
"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.writ e(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(lis t1)
HTH
Mar 15 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
14302
by: Bertram Scharpf | last post by:
Hi, when I write >>> print 'abc', 'def', >>> print 'ghi' I get the output 'abc def ghi\n'. Is there a way to manipulate the print
30
4157
by: Martin Bless | last post by:
Why can't we have an additional 'print' statement, that behaves exactly like 'print' but doesn't insert that damn blank between two arguments? Could be called 'printn' or 'prin1' or 'prinn' anything similar you like. Would make my life so much easier. Often I start with a nice and short print statement, then there happen to be more arguments and neat formatting becomes more important and as a result I find myself
9
22171
by: Paul Watson | last post by:
I thought that using a comma at the end of a print statement would suppress printing of a newline. Am I misunderstanding this feature? How can I use print and not have a newline appended at the end? C:\src\projects\test1>python -c "import sys;print sys.version, 'running on', sys.platform" 2.3.4 (#53, May 25 2004, 21:17:02) running on win32 C:\src\projects\test1>python -c "print 'here'," >jjj
0
1413
by: David Bolen | last post by:
I ran into this strange behavior when noticing some missing spaces in some debugging output. It seems that somewhere in the print processing, there is special handling for string contents that isn't affected by changing how a string is represented when printed (overriding __str__). For example, given a class like: class mystr(str):
14
2906
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version: $Revision: 0.0 $
2
5125
by: jamesthiele.usenet | last post by:
I recently ran into the issue with 'print' were, as it says on the web page called "Python Gotchas" (http://www.ferg.org/projects/python_gotchas.html): The Python Language Reference Manual says, about the print statement, A "\n" character is written at the end, unless the print statement ends with a comma. What it doesn't say is that if the print statement does end with a
11
2067
by: A.M | last post by:
Hi, I found print much more flexible that write method. Can I use print instead of file.write method? Thank you,
10
1582
by: Prisoner at War | last post by:
Hi, your friendly neighborhood n00b here, just wondering why on earth the Py3K folks want to mess with a simple thing like the "print" "command" (is that what it's called, a command?), turning it into "print()"...I mean, what's the point, exactly?? To look like a more "traditional" computer-language format? And what's with not supporting the so-called softspace "feature" of the current "print" command, where a space after a comma, like
7
1418
by: bukzor | last post by:
I was trying to change the behaviour of print (tee all output to a temp file) by inheriting from file and overwriting sys.stdout, but it looks like print uses C-level stuff to do its writes which bypasses the python object/inhertiance system. It looks like I need to use composition instead of inheritance, but thought this was strange enough to note. $python -V Python 2.5
0
8685
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8880
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.