How to use Print Statement in Python

 

Video Tutorial

How to use Print Statement in Python

Reading input from standard input and displaying result on standard output are the basic and importnat functions of any programming language. The aim of this tutorial is to understand how to use print statement in python in detail with simple programming examples.
The print statement can be used in the following different ways :

- print ("Hello World")
- print ("Hello", <Variable Containing the String>)
- print ("Hello %s" % <variable containing the string>)

In Python, single, double and triple quotes are used to denote a string. Usually single quotes are used when declaring a single character or one word. Double quotes re used when declaring a line and triple quotes are used when declaring a more than one line that is a paragraph/multiple lines.

In [1]:
print ('Hey')
Hey
In [2]:
print ("Hello World")
Hello World
In [3]:
print ("""My name is Mahesh Huddar.

I love Python.""")
My name is Mahesh Huddar.

I love Python.

The value of a variable along with string message can also be displayed using print statement.

In [4]:
a = 10
print ("The value of a is ",a)
The value of a is  10
In [5]:
a, b = 10, 20
print ("The Value of a is ", a, "and value of b is ", b)
The Value of a is  10 and value of b is  20

Another method to display string message and value of a varible is using format specifier. %d format specifier is used to refer to a variable which contains a int typed value.

Similarly, when using other data types

- %s -> string
- %d -> Integer
- %f -> Float
- %o -> Octal
- %x -> Hexadecimal
- %e -> exponential

This can be used for conversions inside the print statement itself. In the value of a is 10. The vale of a is printed using format specifier. Second example the value of a i 10 and it is converted to octal, hexadecimal and exponent equivant and displayed using print statement.

In [6]:
a = 10
print ("Hello %d" % a)
Hello 10
In [7]:
a = 18
print ("Actual Number = %d" % a)
print ("Float of the number = %f" % a)
print ("Octal equivalent of the number = %o" % a)
print ("Hexadecimal equivalent of the number = %x" % a)
print ("Exponential equivalent of the number = %e" % a)
Actual Number = 18
Float of the number = 18.000000
Octal equivalent of the number = 22
Hexadecimal equivalent of the number = 12
Exponential equivalent of the number = 1.800000e+01

When referring to multiple variables parenthesis is used. for example,

In [8]:
a, b = 10, 20
print ("Value of a is %d and b is %d" % (a, b))
Value of a is 10 and b is 20

Other Examples

The following are other different ways the print statement can be put to use. In this case %d is a part of string message, hence before %d one more % is used to remove the special mmeaning of %d.

In [9]:
print ("I want %%d to be printed"%())
I want %d to be printed
In [10]:
print ('_A'*10)
_A_A_A_A_A_A_A_A_A_A
In [11]:
print ("Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug")
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
In [12]:
print ("I want \\n to be printed.")
I want \n to be printed.

PrecisionWidth and FieldWidth

Fieldwidth is the width of the entire number to be displayed usinf print statement and precision is the width towards the right, that is number digits after decimal point. One can alter these widths based on the requirements.

The default Precision Width is set to 6. This means after decimal point by default 6 digits are displayed.

In [13]:
print ("%f" % 3.121312312312)
3.121312

Notice upto 6 decimal points are returned.

To specify the number of decimal points, the syntax ‘%(fieldwidth).(precisionwidth)f’ is used. Before . the value represents the total length of number to be displayed and after . the value represents the number of digits after decimal point. In the first example .1 represents one digit to be displayed after decimal point.

In [14]:
"%.1f" % 3.121312312312
Out[14]:
'3.1'

If the field width is set more than the necessary than the data right aligns itself to adjust to the specified values.

In [15]:
"%9.5f" % 3.121312312312
Out[15]:
'  3.12131'

Zero padding is done by adding a 0 at the start of fieldwidth.

In [16]:
"%09.5f" % 3.121312312312
Out[16]:
'003.12131'

For proper alignment, a space can be left blank in the field width so that when a negative number is used, proper alignment is maintained.

In [17]:
print ("% f" % 3.121312312312)
print ("% f" % -3.121312312312)
 3.121312
-3.121312

‘+’ sign can be returned at the beginning of a positive number by adding a + sign at the beginning of the field width.

In [18]:
print ("% +f" % 3.121312312312)
print ("% f" % -3.121312312312)
+3.121312
-3.121312

As mentioned above, the data right aligns itself when the field width mentioned is larger than the actualy field width. But left alignment can be done by specifying a negative symbol in the field width.

In [19]:
print("%-9.3f" % 3.121312312312)
3.121    

Summary

This tutorial discusses the syntax and how to use print statement in python. Format specifier, field width and precision width are also discussed. If you like the tutorial share with your friends.

Leave a Comment

Your email address will not be published. Required fields are marked *