No newline using printf

Gary Herron gherron at islandtraining.com
Fri Sep 16 18:43:38 CEST 2005


Samuel wrote:

>Hello,
>
>I have been searching for an answer for almost two hours now and have
>not found an answer. I want this code:
>
>for i in range(3):
>  print i   # or whatever
>
>To produce this output:
>012
>
>How can I print a word without appending a newline character? Appending
>a "," to the print statement only substitutes the newline for a space,
>which is not what I am looking for.
>
>Any hints?
>
>Thanks,
>-Samuel
>
>  
>
The solution is to take over full control of the output with 
sys.stdout.write.

Use '%1d' % i to convert your number into a single character string.

Use sys.stdout.write to send exactly the characters you want to sys.stdout.

Thus:  sys.stdout.write('%1d' % i) should do what you want.

Dr Gary Herron
Digipen Institute of Technology




More information about the Python-list mailing list