Many people start their programming with simple “Hello World!” program; It has become the traditional first program that people learn. “Hello, world” program simply prints out “Hello World!”. It is simple enough so that people who have no experience with computer programming can easily start programming in a new language.

Hello World” program in Python is very simple, it is one line statement as shown below,

>>> print "Hello, World!"
Hello, World!

Note: This document examples are created using python 2.7. In Python 3+ you should use print statement instead of print command.

Python3 print statement example

print(“Hello, World!”)

>>> print("Hello World!")
Hello World!

Please replace quotes(“) when trying these examples in Python3+.

In above program “Hello, World!” is a string and print is python command to write a text string the characters in quotes to the console or terminal window where we started python. It is useful for sending messages and keeping track of what is happening in the program.

Python print command has different operators using which we can write a string, integer, variables and other data types.

Basic print command examples

print something

Where something is an item or expression of any data type. We can print some data types directly, but most of that can be printed out by the print statement in a straightforward way.

Following are the few examples of print command you can use in your program to write anything on the console or terminal window.

Example1: Printing strings in python

>>> print "Jhon"
Jhon
>>> print "Jhon Kennedy"
Jhon Kennedy
>>> print "Hi, I am Python."
Hi, I am Python.

Above examples shows simple print string or a sentence which enclosed within double-quote marks.

Example2: Printing numbers in python. When playing with a number, we can do simple mathematical with a print statement as shown below. 

>>> print 25
25
>>> print 2*2
4
>>> print 2 + 5 + 9
16

Above examples shows print integer including simple math functions.

Python print command operators

The print statement is useful for joining multiple words, strings, numbers with different data types as well. Below examples shows on how to join multiple strings to form a single sentence. Print statement has operators which are useful for joining same data types, different data types and formatting output in a meaningful way. Below table will show different operators and formatting options available.

Operator Usage
          ,  Used to print multiple strings in a line.
          +  Used to concatenate two strings into single string.
%  Used to concatenate Strings & Integers

Example3: Print command with,(comma) operator

>>> print "Hello,","I","am","John"
Hello, I am John
>>> print "Jhon","is",20,"years","old"
Jhon is 20 years old
>>> print 1,2,3,4,5,6,7,8,9,0
1 2 3 4 5 6 7 8 9 0

The print command has an operator where it can print out more than one item at once on a single line to use this; you separate the items to be printed with commas. As shown in above examples.

Note: Note that comma operator will insert spaces between the items which are printed. There is no way to prevent the spaces from being added if you use commas If you don’t want to see spaces use, plus symbol. Below are available operators with the print statement.

Example4: Print command with +(plus) operator

Python has “+” operator which you can use with print command to concatenate strings and different data types. Following are the few examples of print statement using “+” operator.

>>> print "Hello John, " + "How are you?"
Hello John, How are you?
>>> print "I am 20 Years old " + "and" + " My friend is 21 years old."
I am 20 Years old and My friend is 21 years old.
>>> print "20 " +  "+" +  " 2" + " = " + "22"
20 + 2 = 22
>>> print "My brother is " + 25 + " years old."
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

First three examples prints correct output, but last examples give an error  “TypeError: cannot concatenate ‘str’ and ‘int’ objects.” This is an error python interpreter shows because it is not possible to perform arithmetic on string and integer (numbers). That is numbers can not be concatenated with strings.

On the other hand, there is a function called str() which turns a number into a string representation. So str(20) is the same as “20”. (str(x) is the same as whatever number x equals, but represented as a string instead of a number.), So when we do:

>>> print "My brother is " + str(25) + " years old."
My brother is 25 years old.
>>> print "20 + 2 = " + str(20+2)
20 + 2 = 22

Example5: Python has one more operator “%” we can use with print command to concatenate strings and integers. Following are few examples of “%” operator.

>>> x = "Jenny"
>>> print "%s is my best friend!" %(x)
Jenny is my best friend!

In above example, x is variable and assigned a value “Jenny” which is a string, and when you add “%s” as an additional parameter in your print statement and at the end, “%x” is nothing but the variable value we are passing as a string. There is more we can do with “%.” We can use with integers and float values.

 %s  Used to print String
 %d  Used to print integers
    %f  Used to print floating point integers
>>> x = 16
>>> print "My younger brother is %d years old." %(x)
My younger brother is 16 years old.
>>> x = 1.5
>>> print "Price of dark chocolate is %.1f $." %(x)
Price of dark chocolate is 1.5 $.

Above examples shows how to print integers and float values with “%” operator. Let’s try a complex case.

>>> a = 3.0
>>> b = 2008
>>> c = 2
>>> d = 2.7
>>> x = "mid-2010"
>>> print "Python %.1f was released in %d. The final %d.x version %.1f release came out in %s." % (a,b,c,d,x)
Python 3.0 was released in 2008. The final 2.x version 2.7 release came out in mid-2010.

Example6: How about escape characters in python print statement.  

When you want to print something on new line you can use print command operator “n,” similarly you can print tab-separated strings using “t”. The following table shows two most important type of print operator in python.

Escape Character Usage
                n  Used to print on new line
             t  Used to print tab separated line
>>> print "Hi,nMy name is Jhon,nand i am 20 years old."
Hi,
My name is Jhon,
and i am 20 years old
>>> print "Hello, This is TABtseparated string."
Hello, This is TAB    separated string.

Hope this will help you to start with python programming. In all these statements, print with + is bit different and frequently used.

 

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.