[Tutor] Comma at the end of a print command

Kent Johnson kent_johnson at skillsoft.com
Sun Aug 15 02:18:07 CEST 2004

Output with print will always separate values with spaces. If you want 
finer control you can write directly to sys.stdout (which is what print 
does). For example:

 >>> print 1,;print 2
1 2
 >>> import sys
 >>> sys.stdout.write('1');sys.stdout.write('2')

Note that write() will never append a newline to the string you give it; if 
you want a newline you have to specify it yourself. Also note that a more 
common way to eliminate spaces in print statements is to format your output 
with %, but that won't work in this case.


At 07:04 PM 8/14/2004 -0400, Dragonfirebane at aol.com wrote:
>Hello all,
>I know that when you put a comma at the end of a print command, the next 
>printed object will be printed on the same line, separated by a space. Is 
>there a way to do this without the space separation? i.e.:
>def mod():
>     import msvcrt
>     model = msvcrt.getch()
>     if model == '1':
>         print model(*)
>         model = msvcrt.getch() ##to get the next number
>         print model
>would output:
>, printing the numbers one at a time, if '12' were inputed. as it is, the 
>output is:
>1 2
>, which isn't what i want. So in other words: is there a way to provide 
>'real-time' printing without the space separation?  The reason I'm using 
>msvcrt.getch() is because if the number doesn't begin with a 1, then it 
>must be below 10, as the choices are from 2 to 15; which would make it 
>easier (no need to hit enter) to process the choice.
>* insert unknown symbol here
>Email: dragonfirebane at aol.com
>AIM: singingxduck
>Programming Python for the fun of it.
>Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list