You Are Here Home > Print without a new line or space in Python

Print without a new line or space in Python

Here is another little trick in Python; the print statement always inserts a line break after it prints your text, for example:

print "Hello"
print "Python"
print "!"

Will print:
Hello
Python
!

Now, you can fix the issue with the line break by inserting a comma after the print statement:

print "Hello",
print "Python",
print "!"

This will print:
Hello Python !

But the problem is that it will still insert a space after it prints the text, so to fix this you will have to use sys.stdout.write like this:

import sys
sys.stdout.write("Hello ")
sys.stdout.write("Python")
sys.stdout.write("!")

This will print:
Hello Python!

Have fun with Python :)

Print without a new line or space in Python
Filed under: Python   Posted by: Codehead on November 6, 2008

Disclaimer
1 - Use the information provided here at your own risk.
2 - You may not republish this content without prior written consent.




9 Comments »

  1.  

    For Python 3.0 the print statement is now a function.

    print “Hello!” is now print(“Hello!”)

    Your space-less example would be obtained by using the sep paramater.

    print(“Hello “, “Python”, “!”, sep=”")

    Comment — November 6, 2008 @ 3:58 pm

  2. Codehead:
     

    Jake, that’s good to know, thanks!
    I didn’t have time to look at Python 3.0 yet.

    Comment — November 6, 2008 @ 4:11 pm

  3. nick:
     

    in Python 5, the print statement can be used as a statement, or a function. It doesnt matter.

    Comment — December 16, 2008 @ 7:24 pm

  4. Codehead:
     

    Python 5?!

    Comment — December 16, 2008 @ 7:41 pm

  5. Eric J:
     

    Thanks, This was very clear and concise. I’m glad that people like you take the time to post stuff like this so people like me don’t have to take the time to crack a book!

    Comment — June 11, 2009 @ 10:39 am

  6. russell:
     

    LOL python 5? typo? 3 is the latest public version

    Comment — September 4, 2009 @ 3:16 pm

  7.  

    No, he is right, python 5 supports print as a statement or a function. I think its rather useless that they would restrict you in python 3

    Comment — September 6, 2009 @ 11:58 am

  8. neha:
     

    I have a little doubt here…
    this post here solved 50% of my problem…
    disclaimer : i am a beginner :)

    In C/c++ you have functions to respond to keyboard interupt…like getch()…hence,the moment you tap on a key..it returns a value or gives control main()..
    What is the equivalent method in python to read keyboard interrupts?

    Also,this does not really solve it yet,
    I need to print a ‘[' and then read a string and then immediately print another ']‘ without any spaces.
    But the way python appears to work , until and unless i hit enter , it does not move to the next line of code.Hence no matter what i do i get the ‘]’ on the next line.
    Please help me out here , i seem to be missing out on something.

    the piece of code:

    i=0
    a=[ '*' , '*' , '*' ],[ '*' , '*' , '*' ],[ '*' , '*' , '*' ]
    for i in range(0,3):
    	print a[i]
     
    print "INPUT"
    for i in range(0,3):
    	print a[i]
     
    for i in range(0,3):
            for j in range(0,3):
                    sys.stdout.write('[')
                    a[i][j]=sys.stdin.read(1)
                    sys.stdout.write(']')

    Comment — October 28, 2009 @ 5:47 am

  9. Jinsong Huang:
     

    For unbuffered read, see this thread.

    http://code.activestate.com/recipes/134892/

    Comment — November 4, 2009 @ 2:35 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment