print "foo", without a space 
Author Message
 print "foo", without a space

Hello All!

Is there another way of getting print to not make a newline than
using:
    print "foo",  ?
This because the comma always produces a space which necessary
isn't that nice, for example:

for i in range (1,30):
    print i,
    if i%10==1 and i<>11: print "st",
    elif i%10==2 and i<>12: print "nd",
    elif i%10==3 and i<>13: print "rd",
    else: print "th",
    print "fish"

Now you would like the "endings" to come right after the number.
There is naturally always some way to get around this, in this
case for example:

end=["st","nd","rd","th"]
for i in range(1,30):
    print `i`+end[max(min(abs(i)%10,4)-1-
    4*(not(int('0'+`abs(i)`[-2:-1])-1)),-1)],'fish'

OK, this got a bit out of hand, the above looks terrible, does
anyone see how it could be simplified? At least it's more general
than the above as it can handle the whole range of integers,
including negative ones :-) However it shows that it would be
less obfuscating to have a print statement that could continue
printing on the same line without a space.

If there isn't an easy way for this, I suggest the following:
    print "foo".
So that a dot at the end of the line would mean "no space either".

Appreciating any comments.

Have a nice day,
    Mikael J.
    for e-mail note that the country code for Finland is not xx



Mon, 29 Apr 2002 03:00:00 GMT  
 print "foo", without a space

Quote:

> Hello All!

> Is there another way of getting print to not make a newline than
> using:
>     print "foo",  ?

I think there's some way invloling sys.stdout.softspace, but I'm no
sure what. However...

Quote:
> This because the comma always produces a space which necessary
> isn't that nice, for example:

> for i in range (1,30):
>     print i,
>     if i%10==1 and i<>11: print "st",
>     elif i%10==2 and i<>12: print "nd",
>     elif i%10==3 and i<>13: print "rd",
>     else: print "th",
>     print "fish"

> Now you would like the "endings" to come right after the number.
> There is naturally always some way to get around this, in this
> case for example:

> end=["st","nd","rd","th"]
> for i in range(1,30):
>     print `i`+end[max(min(abs(i)%10,4)-1-
>     4*(not(int('0'+`abs(i)`[-2:-1])-1)),-1)],'fish'

> OK, this got a bit out of hand,

!

Quote:
> the above looks terrible, does
> anyone see how it could be simplified? At least it's more general
> than the above as it can handle the whole range of integers,
> including negative ones :-) However it shows that it would be
> less obfuscating to have a print statement that could continue
> printing on the same line without a space.

Do you know about the string % operator? Try:

def ordinate(n):
    if n%10 == 1 and n <> 11:
        return "st"
    if n%10 == 2 and n <> 12:
        return "nd"
    else:
        return "th"

print "This is the %d%s fish"%(i,ordinate(i))

It's generally the way to go for formatting output.

Quote:
> If there isn't an easy way for this, I suggest the following:
>     print "foo".
> So that a dot at the end of the line would mean "no space either".

With all due respect: no chance.

Quote:
> Appreciating any comments.

Hoping this helps,
Michael


Mon, 29 Apr 2002 03:00:00 GMT  
 print "foo", without a space
Quote:
Michael Hudson writes:


 >
 > > Hello All!
 > >
 > > Is there another way of getting print to not make a newline than
 > > using:
 > >     print "foo",  ?
 >
 > I think there's some way invloling sys.stdout.softspace, but I'm no
 > sure what. However...

Another option is to forego the "print" and use "sys.stdout.write()"
instead.  Conversion of the argument to a string is your
responsibility, and no extra spaces/newlines/etc are added to the
output.



Mon, 29 Apr 2002 03:00:00 GMT  
 print "foo", without a space
Nothing with print comes to mind off hand, but you could just use
sys.stdout.write() instead (it wont insert a space).

Enjoy,
Arad
--

NT Platform and I/O Team
Software and System Development Lab
Hewlett-Packard

Quote:

>Hello All!

>Is there another way of getting print to not make a newline than
>using:
>    print "foo",  ?
>This because the comma always produces a space which necessary
>isn't that nice, for example:

>for i in range (1,30):
>    print i,
>    if i%10==1 and i<>11: print "st",
>    elif i%10==2 and i<>12: print "nd",
>    elif i%10==3 and i<>13: print "rd",
>    else: print "th",
>    print "fish"

>Now you would like the "endings" to come right after the number.
>There is naturally always some way to get around this, in this
>case for example:

>end=["st","nd","rd","th"]
>for i in range(1,30):
>    print `i`+end[max(min(abs(i)%10,4)-1-
>    4*(not(int('0'+`abs(i)`[-2:-1])-1)),-1)],'fish'

>OK, this got a bit out of hand, the above looks terrible, does
>anyone see how it could be simplified? At least it's more general
>than the above as it can handle the whole range of integers,
>including negative ones :-) However it shows that it would be
>less obfuscating to have a print statement that could continue
>printing on the same line without a space.

>If there isn't an easy way for this, I suggest the following:
>    print "foo".
>So that a dot at the end of the line would mean "no space either".

>Appreciating any comments.

>Have a nice day,
>    Mikael J.
>    for e-mail note that the country code for Finland is not xx



Mon, 29 Apr 2002 03:00:00 GMT  
 print "foo", without a space
[Mikael Johansson]

Quote:
> Is there another way of getting print to not make a newline than
> using:
>     print "foo",  ?

No.

Quote:
> This because the comma always produces a space which necessary
> isn't that nice ...

That's a different issue than newlines.  In small doses, this devious trick
is quite pleasant:

Quote:
>>> import sys
>>> def no_space_before(x):

        sys.stdout.softspace = 0
        return x

Quote:
>>> ns = no_space_before
>>> print 1, ns(2), 3, ns(4), 5
12 34 5

the-great-thing-about-python-is-it's-so-obvious<wink>-ly y'rs  - tim


Tue, 30 Apr 2002 03:00:00 GMT  
 print "foo", without a space

Hello All!

Quote:
> > Is there another way of getting print to not make a newline than
> > using:
> >     print "foo",  ?

> No.

OK, thanks to all who replied, this was what I really wanted to know.
The other tricks where known to me, but the .softspace thingie seemed
interesting (even if not that usable :-)

The reason for me wanting the print-statement to not make an auto-
space was just to have one simple output-command.

The reason for this again is that I've been assisting in a
thermodynamics course here at the university. I've been responsible
for trying to get the students to do some small proggies (calc.
Gibbs energies etc.), and none of them had any programming experience
to start with, so I chose python as it seemed (and still seems) a
nice and quite easily adoptable language. I hadn't programmed Python
before either, but now I love it :-)  But anyway, the concept of
stdout and so on are a bit too complicated to get through to the
students in the very limited time I have (it's not supposed to be a
programming course after all).

Have a nice day,
    Mikael J.



Tue, 30 Apr 2002 03:00:00 GMT  
 print "foo", without a space

Quote:

> Is there another way of getting print to not make a newline than
> using:
>     print "foo",  ?
> This because the comma always produces a space

[snip]

have you tried

        sys.stdout.write( "foo" )

--
Dale Nagata         |  tel : +1 604.451.2700 ext. 2254 (UTC-0800)
Software Developer  |  fax : +1 604.437.9891
Creo Products Inc.  |  pgr : +1 604.691.8279
Burnaby BC Canada   |  http://www.creo.com/



Tue, 30 Apr 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. A use for "foo = foo()"

2. $foo == "" or [llength $foo] == 0

3. "import foo" and "from foo import bar"

4. "foo"

5. #pragma interface(C,"foo")

6. Dpulicating "class Foo:" in C

7. "exception in foo.__del__() ignored" strangeness

8. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

9. "out of space" error

10. "Get Disk Free Space" for NTFS

11. Gnat301a "Out of Space" msg

12. What is "largest relative spacing"?

 

 
Powered by phpBB® Forum Software