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
I'm the co-founder of
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
Jake, that’s good to know, thanks!
I didn’t have time to look at Python 3.0 yet.
Comment
in Python 5, the print statement can be used as a statement, or a function. It doesnt matter.
Comment
Python 5?!
Comment
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
LOL python 5? typo? 3 is the latest public version
Comment
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
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:
Comment
For unbuffered read, see this thread.
http://code.activestate.com/recipes/134892/
Comment
Thankx helped a lot
Comment
Why not just:
print(‘foo’, end=”)
print(‘bar’)
Will print: “foobar”.
Comment
I’m pretty sure that you can only do that in Python 3…
Comment
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
print “Hello”,
Comment
Thank you. I like it.
Comment
Why don’t you just do
print “Hello\nWorld\n!” ?
Comment
can you help me insert a newline in this string(name=fname+lname+tname+kname+mname
)
Comment
Comma does not work for me.
See: http://docs.python.org/py3k/library/functions.html#print
Comment