Home > Articles > Open Source > Python

This chapter is from the book

Printing Strings

In the examples so far, Python prints out strings with the quotes still around them. If you want to get rid of these quotes, use a print statement:

>>> greeting = "Hello"
>>> print greeting
Hello

A print statement usually prints out the string, then moves to the next line. What if you don’t want to move to the next line? In this case, you can add a comma (,) to the end of the print statement. This signals Python not to move to a new line yet. This only works in a file, though, because the shell will always move to the next line.

In this example, we print out an item along with the price on the same line:

print 'Apple: ',
print '$ 1.99 / lb'

When we run it, we get this:

Apple:  $ 1.99 / lb

We can even do calculations between the two print statements, if we need to. Python will not move to a new line until we tell it to.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.