Understanding How Print Statements in Python Work

87

The print() function in Python is used to print the specified string (or) object either on screen, i.e., standard output device, or in a text stream file. Let’s look into the syntax of the print() function and its parameters in detail.

Also ReadHow to Create a Neural Network in Python

Understanding Different Print Statements in Python

Syntax: print(object(s), sep=’ ‘, end=’\n’, flush=False, file = sys.stdout)

Print() Parameters

  • object(s): It represents the object to be printed. The print() function can accept more than one object.
  • Sep: sep indicates the separator by which objects are separated. It is an optional parameter; by default, it considers ‘ ‘ as a separator.
  • End: the end is printed at last. It’s an optional parameter, and the default value for the end parameter is ‘\n’.
  • Flush – It is used to clear the internal buffer/stream. It’s an optional parameter and accepts boolean values (True/False). The default value is False.
  • File – Accepts an object which has write permission on it. It’s an optional parameter, and the default value is sys.stdout

Returns none. It just prints object data in the output screen/file.

Note: The object is converted into a string before being written to the screen.

Let’s look into a few example programs on how the print statements work in Python with different parameters.

Also ReadOr Operator in Python

Using print() to display the output

A simple print() function that prints data in the output screen.

Code

print('This is CodeITBro website')

number=1000

print('number-',number)

languages= ['C', 'C++', 'Java', 'Python']

print(languages)

Output

This is CodeITBro website

number- 1000

[‘C’, ‘C++’, ‘Java’, ‘Python’]

Explanation: In the above code, we only specified the object parameter in the print () function without other parameters. So they all are set to default values, i.e., sep=’ ‘, end=’\n’, file=sys.stdout, flush=False.

Also ReadNatural Language Processing in Python: Techniques

Separating data in objects using the print() sep parameter

In this example, let’s separate the data in an object by specifying the separator to the sep parameter.

Code

string1 = 'CodeITBro'

string2 = 'learning'

string3 = 'Website'

# printing multiple objects without specifying sep

print(string1,string2,string3)

# printing multiple objects with a separator

print(string1,string2,string3,sep='_')

Output

CodeITBro learning Website

CodeITBro_learning_Website

Also ReadPython Reserved Words List With Definitions and Examples

Explanation: From the output, we can get an insight that:

  • In the first print statement, we didn’t specify a separator, so by default, it separated the three objects with a space (‘ ‘).
  • In the second print statement, we passed a sep parameter to the print() function with ‘_’ assigned. So the separator separated the three objects in the print function.

Also ReadHow to Fix Invalid Syntax Error in Python

End parameter in print() function

In this example, let’s try to use the end parameter in print() function to know how it works.

Code

# list of programming languages
languages= ['C', 'C++', 'Java', 'Python']

print('Printing list of data without end specified in print()')
for i in languages:
    # printing each item without specifying end
    print(i)

print('\nPrinting list of data with end specified in print()')
for i in languages:
    # printing each item in list object by specifying end
    print(i, end=',')

Also ReadHow to Fix Unexpected EOF While Parsing Error In Python

Output

Printing list of data without end specified in print()

C

C++

Java

Python

Printing list of data with end specified in print()

C,C++,Java,Python,

Also ReadHow To Copy List in Python

Explanation:

  • In the first print statement, we didn’t specify an end parameter. So the print() function considers the default value for the end parameter as ‘\n’. This results in printing each item in a list in a new line.
  • In the second print statement, we passed an end parameter to the print() function with the value ‘,’ assigned. So after each item in the list Python interpreter will append ‘,’ in the output screen.

Also Read10 Best Programming Apps To Learn Python

File parameter in print() function

In this example, let’s try to know how the file parameter in print() function works with a sample code.

Code

# opening a demo file in write mode

demoFile = open(r'C:\Users\v.akhil\Desktop\demoFile.txt','a')

# printing the data in the output screen

print('Printing data to output screen')

# printing data into the file object

print('An online learning website', file = demoFile)

print('Data added in file')

demoFile.close()

Also ReadPython Program To Reverse A Number

Output

Printing data to the output screen

Data added in file

demoFile.txt – Before running the code

printing data in a txt file

demoFile.txt – After the code run

print statements in python

Also ReadPython Program to Find Sum of Digits [5 Methods]

Explanation:

  • We didn’t mention the file parameter in print () function in the first print statement. So it considers the default value sys.stdout, which means that the print()function adds the text to the display screen.
  • In the second case, we passed the file parameter to the print() function with a value assigned, i.e., a file object. So Python Interpreter adds the data specified in print () function to the file mentioned in the fileobject. Hence the “An online learning website” is appended into the ‘demoFile.txt’ file after the code run.

Also ReadSending Emails Using Python With Image And PDF Attachments

Summary

Here we looked at the syntax of the print() function and the function of the different parameters that can accept detailed examples for each. So from the above examples, we can understand how the print() statements work in Python.