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: CodingRecipes

20 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

     
  2. Codehead:
     

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

    Comment

     
  3. nick:
     

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

    Comment

     
  4. Codehead:
     

    Python 5?!

    Comment

     
  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

     
  6. russell:
     

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

    Comment

     
  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

     
  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

     
  9. Jinsong Huang:
     

    For unbuffered read, see this thread.

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

    Comment

     
  10. Venu gopa rao:
     

    Thankx helped a lot

    Comment

     
  11.  

    Why not just:
    print(‘foo’, end=”)
    print(‘bar’)

    Will print: “foobar”.

    Comment

     
  12. Codehead:
     

    I’m pretty sure that you can only do that in Python 3…

    Comment

     
  13. Eduardo:
     

    Hi everybody!

    I have al litle problem with this, when I use, I get in output a “None” and a line break after what I want print. Maybe beacause i´m usign this inside a while but i´m not shure. Any idea?

    Thanks.

    Comment

     
  14. vanuan:
     

    print “Hello”,

    Comment

     
  15. Thank you:
     

    Thank you. I like it.

    Comment

     
  16. mike:
     

    Why don’t you just do

    print “Hello\nWorld\n!” ?

    Comment

     
  17. sola:
     

    can you help me insert a newline in this string(name=fname+lname+tname+kname+mname
    )

    Comment

     
  18. py3k:
     

    Comma does not work for me.
    See: http://docs.python.org/py3k/library/functions.html#print

    Comment

     
  19. koosha:
     

    thank u

    Comment

     
  20. Said:
     

    Thank you so much

    Comment

     

RSS feed for comments on this post. TrackBack URL

Leave a comment