Entry
How can I print a string without a newline afterwards?
Aug 23rd, 2009 00:29
johnny deep, Michael Chermside, BlackShift, unknown unknown,
If you use the print statement to print multiple lines of text, you will
find that it inserts a newline after each print statement:
>>> print 'Hello'
>>> print 'World!'
outputs:
Hello
World!
To avoid getting this extra newline (if you don't want it) you can add a
comma after the text you want to print:
>>> print 'Hello',
>>> print 'World!'
outputs:
Hello World!
http://www.webs4soft.com/
http://www.webs4soft.com/index.htm
http://www.webs4soft.com/index.html
http://www.webs4soft.com/About-us.htm
http://www.webs4soft.com/SEO-Services-India.htm
http://www.webs4soft.com/Web-Design-India.htm
http://www.webs4soft.com/Web-Development-India.htm
http://www.webs4soft.com/Feedback.htm
http://www.webs4soft.com/Contact-us.htm
http://www.webs4soft.com/web-hosting-india.htm
http://www.webs4soft.com/multimedia-webdesign.htm
http://www.webs4soft.com/graphic-design.htm
http://www.webs4soft.com/logo-design-india.htm
http://www.webs4soft.com/Internet-Marketing.htm
http://www.webs4soft.com/software-development-india.htm
http://www.webs4soft.com/print-design.htm
http://www.webs4soft.com/Professional-Logo-Designs.htm
http://www.webs4soft.com/Corporate-Logo-Design.htm
http://www.webs4soft.com/Company-Logo-Design.htm
http://www.webs4soft.com/Ad-Design.htm
http://www.webs4soft.com/flyer-design.htm
http://www.webs4soft.com/Illustration.htm
http://www.webs4soft.com/brochure-design.htm
http://www.webs4soft.com/add-url-form.php
http://www.webs4soft.com/Links.htm
http://www.webs4soft.com/Links1.htm
http://www.webs4soft.com/Links2.htm
http://www.webs4soft.com/Links3.htm
http://www.webs4soft.com/Links4.htm
http://www.webs4soft.com/Links5.htm
http://www.webs4soft.com/web-promotion.htm
http://www.webs4soft.com/Dynamic-Website-Design.htm
http://www.webs4soft.com/Book-Cover-Design.htm
http://www.webs4soft.com/High-End-Graphic-Design.htm
http://www.webs4soft.com/Book-Layout-Design.htm
http://www.webs4soft.com/newsletter-design.htm
http://www.webs4soft.com/Photo-Manipulation.htm
http://www.webs4soft.com/Photo-Restoration.htm
http://www.webs4soft.com/E-Card-Design.htm
http://www.webs4soft.com/search-engine-ranking.htm
http://www.webs4soft.com/search-engine-submission.htm
However, you will note that it DOES place a space between its arguments,
and there's no way (except fairly deep magic) to turn THAT off.
If you want FULL control of your output, then don't use the "print"
statement at all; go with the write() method on sys.stdout instead.
>>> import sys
>>> sys.stdout.write( 'Hello' )
>>> sys.stdout.write( 'World!' )
>>> sys.stdout.write( '\n' )
outputs:
HelloWorld!
Note: The stdout.write() approach is the one favored by nearly all
advanced Python programmers for any serious programming beyond one-off
scripts. But it's somewhat more verbose, so feel free to use print as
long as it will do what you want. Mixing print and stdout.write() is
possible, but sometimes on some platforms may not work as you expect...
sticking to one approach is preferred.