Methods and Functions

Python print()

One of the most basic and popular functions in Python – the print() statement. We all might have started learning Python programming with the evergreen example of printing ‘Hello World’. The Python print() method is used to obtain output i.e. display anything on the screen as well as to debug code. This function outputs the supplied message or a value to the console. But if you think that’s all there is to know about Python’s print() method, you’re mistaken! This article focuses more on the in-depth functionalities of the Python print() function.

Python print() function

Definition

  • Python print() function is used to print the specified object to the standard output device (screen) or to a text stream file.
  • The Python print() method is used to print a given message to the screen or to display an object’s value on the terminal.

 Python print()

Let us understand Python print() by looking at a very basic example.

                    

print('Hello World!')

# tells us that print() is a function and not a statement
print(type(print))

Output

                    

Hello World!
<class 'builtin_function_or_method'>

Unlike Python2, which did not require you to use parentheses, Python3 requires parentheses to be used or it will generate a syntax error. The print() function, which accepts zero or more expressions separated by commas, is the simplest way to generate output. Before writing to the screen, this function turns the expressions you pass into a string.

print() Syntax

The entire syntax of Python print() is as follows:

                    

print(*objects, sep = ' ', end = '\n', file = sys.stdout, flush = False)

print() Parameters

*objects – indicates one or more values/objects to be printed. It is converted to string before printing.

sep (Optional) – this specifies how objects should be separated if multiple objects are passed. Default value: ‘ ‘

end (Optional) – specifies what is to be printed at last. Default value: \n

file (Optional) – object with a write(string) method. Default value: sys.stdout

flush (Optional) – A Boolean indicating whether if the stream/output is flushed (True) or buffered (False). Default value: False

Note – The sep, end, file, and flush parameters are keyword arguments. If we want to use them in the function, we need to specify their keywords compulsorily. For example:

                    

print(*objects, sep = 'separator', end = '\n')
# and not like this
print(*objects, 'separator', '\n')

Return value from print()

The print() function returns output to the screen.

Example 1: How print() works in Python?

Example: Passing single argument to the print() function

                    

# Python program to illustrate print()
# Passing single argument
print(10)

print(12.35)

print('Python Programming')

print(True)

print(1 + 5j)

my_list = [1, 2, 3, 4]
print(my_list)

my_dict = {1: 'One', 2: 'Two', 3: 'Three'}
print(my_dict)

Output

                    

10
12.35
Python Programming
True
(1+5j)
[1, 2, 3, 4]
{1: 'One', 2: 'Two', 3: 'Three'}

Example: Passing multiple arguments to the print() function

                    

# Python program to illustrate print()
# Passing multiple arguments
name = 'Sam'
print('My name is', name)

age = 30
print('My age is', age)

a = 'Amy'
b = 'Tom'
print(a, 'and', b, 'are coming to the party')

x = 5
y = 10
print('The sum of', x, 'and', y, 'is:', x+y)

Output

                    

My name is Sam
My age is 30
Amy and Tom are coming to the party
The sum of 5 and 10 is: 15

In the above 2 examples notice that,

The separator ‘ ‘ is used between multiple objects. Take note of the gap between the two objects in the output.

The character ‘\n’ (newline) is used as the end parameter. Each print command, as you can see, displays the output in a new line.

The default value used for the file parameter is sys.stdout. The result is displayed on the screen.

The flush default value is False. The stream is not flushed forcibly.

Example 2: print() with separators and end parameters

Example

                    

# Python program to illustrate print()
# Passing sep and end parameters
num = 7
print('James Bond ', num, sep = '--> 00', end = '\n\n\n')

a = 2
print('Value of a: ', a, sep = '000', end = '\n')

print('Computer', 'Downloads', 'Wallpapers', 'Img1.png', sep = '/')

Output

                    

James Bond --> 007


Value of a: 0002
Computer/Downloads/Wallpapers/Img1.png

In the preceding program, we passed the sep and end parameters.

Example 3: print() with file parameter

The file parameter allows you to save the result of the print function to a file in various formats such as .csv, .txt, and so on. Here you can specify the file to which you want to write or append the output of the print function.

Example

                    

# Python program to illustrate print()
# Passing file parameters
myfile = open('Student.txt', 'w')
print('This file contains student details', file = myfile)
myfile.close()

This program attempts to open the Student.txt file in writing mode. If this file does not exist, a new file, Student.txt, is generated and opened in writing mode.

In this case, we’ve passed a myfile object to the file parameter. In the Student.txt file, the string object ‘This file includes student details’ is printed.

Finally, the close() method is used to close the file.

Example 4: Printing different data types in Python

                    

num = 12

# Integer
print('Number: %d' %num)

#Float
print('Number: %f' %num)

# Exponential
print('Number: %e' %num)

# Octal
print('Number: %o' %num)

# Hexadecimal
print('Number: %x' %num)

Output

                    

Number: 12
Number: 12.000000
Number: 1.200000e+01
Number: 14
Number: c

Frequently Asked Questions

Q1. What is print() in Python?

One of the most basic and popular functions in Python – the print() statement. The Python print() method is used to obtain output i.e. display anything on the screen as well as to debug code. The Python print() method is used to print a given message to the screen or to display an object’s value on the terminal.

Q2. How do you print in Python?

The print() function, which accepts zero or more expressions separated by commas, is the simplest way to generate output. The entire syntax of Python print() is as follows:

                    

print(*objects, sep = ' ', end = '\n', file = sys.stdout, flush = False)

Example

                    

print(100)

print('Welcome to School')

salary = 10000
print('Your salary is:', salary)

print('Car', 'Bike', 'Train', 'Plane', sep = ' | ', end = '\n\n')

print('Hello', 'World', end = '\n')

Output

                    

100
Welcome to School
Your salary is: 10000
Car | Bike | Train | Plane

Hello World

Q3. How do you print a new line in Python?

To print on a new line, the ‘\n’ newline character is used in the print() function. This newline character can also be applied with the sep and end parameters of the Python print() function.

Example

                    

# new line in the objects parameter
print('Hey buddy! \n How are you?')

# new line using sep parameter
print('Tom', 'Sam', 'Kim', 'Kylie', sep = '\n')

# new line using end parameter
print('Welcome', end = '\n')
print('to the Museum')

Output

                    

Hey buddy!
  How are you?
Tom
Sam
Kim
Kylie
Welcome
to the Museum

Q4. How do you use %s and %d in Python?

Python strings and integers can also be printed using the ‘%s’ and ‘%d’ characters respectively. This is another alternative to print the variable values wherever we want in the print() statement sequence.

Example

                    

language = 'Python'
print('I have learned %s language during my course' %language)

age = 20
print('My age is %d' %age)

Output

                    

I have learned Python language during my course
My age is 20

In the above print statements, we have implemented the ‘%s’ and ‘%d’ characters. To execute this method successfully, we use the ‘%variable_name’ after the objects parameter in the print(). And notice that we do not use any comma’s between the 2 parameters.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.