What does print() function do in Python?


In Python, among several functions and other tools that are used to achieve certain functionalities, the print() function happens to be a basic tool for displaying output to the console or terminal. This function allows programmers to achieve several tasks such as, among others, presenting information, messages, variables, and other data to users or for debugging purposes. In this article, we will investigate in detail the functionality and usage of the print() function in Python, through several code examples followed by comprehensive explanations. This will help you to add one more Python skill to your repertoire of code expertise

Basic Usage of the print() Function

In order to be able to use the print() function, follow these steps:

  • Step 1: You begin by providing the content or variable you want to display within the parentheses of the print() function.

  • Step 2: As an option, you separate multiple items with commas if you want to display them together.

Example

In the example, here, the print() function is used to display the message, "Hello, World!", and the value of the name variable, which is "Lucy". The content within parentheses of the print() function is enclosed in double quotes to indicate a string.

# Display a simple message
print("Hello, World!")

# Display the value of a variable
name = "Lucy"
print("Welcome,", name)

Output

Hello, World!
Welcome, Lucy

Formatting Output with the print() Function

It must be acknowledged that the print() function supports several formatting options to enhance the output. Here we discuss a few commonly used formatting techniques:

Concatenation

Here, you can concatenate or combine strings and variables using the + operator.

Example

name = "Jake"
age = 25
print("Name: " + name + ", Age: " + str(age))

Output

Name: Jake, Age: 25

String Interpolation

You can also embed variables within a string using curly braces {} and the format() method.

Example

name = "Phil"
age = 30
print("Name: {}, Age: {}".format(name, age))

Output

Name: Phil, Age: 30

F-strings: This functionality was introduced in Python 3.6, f-strings provide a concise and convenient way to format output.

Example

name = "Lucas"
age = 35
print(f"Name: {name}, Age: {age}")

Output

Name: Lucas, Age: 35

These formatting techniques discussed above make it possible and allow you to combine strings and variables in a structured manner to create meaningful and useful output.

Printing with Separator and End Options

The print() function, which must be taken note of, also provides additional options to control the separator between items and the end character at the end of the line.

Separator

By default, in the print function, the separator between multiple items is a space. You can customize it using the sep parameter.

Example

name = "Eva"
age = 40
print(name, age, sep=", ")

Output

Eva, 40

End Character

Also in the print function, by default, the end character at the end of the line is a newline (\n). You can change it using the end parameter.

Example

print("Hello", end=" ")
print("World!")

Output

Hello World!

In the example we have discussed above, the first print() statement ends with a space using the end=" " parameter, and the second print() statement continues on the same line.

Printing Numbers with Formatting

It is heartening to note that the print() function can be used to display numbers with specific formatting options. Here are a few examples that are discussed below:

Specifying Decimal Precision:

You should know that you can use the format() method with the: Nf format specifier to specify the number of decimal places to display.

Example

In the example below, the print() function displays the value of pi with 2 decimal places using the:.2f format specifier. The output will be "The value of pi is: 3.14".

pi = 3.14159265359
print("The value of pi is: {:.2f}".format(pi))

Output

The value of pi is: 3.14

Displaying Numbers in Scientific Notation

You must know that you can always use the e-format specifier to display numbers in scientific notation

Example

Here, the print() function uses the e-format specifier with the:2e formatting to display the number in scientific notation with 2 decimal places. The output will be "The number in scientific notation is: 1.23e+09".

num = 1234567890
print("The number in scientific notation is: {:.2e}".format(num))

Output

The number in scientific notation is: 1.23e+09

Printing Multiple Lines with Line Breaks

The print() function, has several functionalities, among them, it can be used to display multiple lines of text by incorporating line breaks.

Example

In this particular example, the text variable contains multiple lines of text separated by the newline (\n) character. The print() function displays the text as it is, preserving the line breaks.

text = "This is line 1.\nThis is line 2.\nThis is line 3."
print(text)

Output

This is line 1.
This is line 2.
This is line 3.

This example teaches us that by incorporating formatting options and line breaks, you can customize and tailor the way numbers and text are displayed using the print() function in Python.

The print() function is one of the most utilitarian and basic tools in Python that allows you to display output to the console. In this present article, we have learned about the basic usage of the print() function, the different formatting options for output, and controlling the separator and end character. We also saw additional examples of deploying the print() function to format numbers with decimal precision or scientific notation and to show multiple lines of text. By diligently following and practicing the code examples provided along with their easy-to-understand explanations, you can enhance or augment your output presentation and create more readable and informative output utilizing the print() function in Python.

Updated on: 13-Jul-2023

444 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements