The Print Function in Python

Introduction

The print function in Python is a built-in function that allows you to display messages or the values of variables to the console. It’s one of the most basic and essential functions you’ll use in Python programming.

The basic syntax for the print function is:

print("Hello World!")

This will print the string “Hello World!” to the console. You can also concatenate multiple strings using the ‘+‘ operator. Let’s see an example:

print("Hello" + " " + "World!")

It will also print the same message as the above: “Hello World!“.

Printing Variables

Let’s see how you can print values from variables using the print function:

name = "Subhankar"
age = 25
print("My name is", name, "and I am", age, "years old.")

The above code will print the string “My name is Subhankar and I am 25 years old.” Notice that we didn’t use string concatenation in this case, but instead used commas to separate the variables.

Printing Expressions

You can also print expressions using the print function. For example:

x = 3
y = 5
print("The sum of", x, "and", y, "is", x + y)

This will print the string “The sum of 3 and 5 is 8”. Notice that we used the ‘+‘ operator to calculate the sum of the variables inside the print function and included it in the string.

Printing with Formatting

Another way to print variables and expressions is by using string formatting. Python supports several methods for string formatting, including f-strings, the ‘%‘ operator, and the ‘format()‘ method.

Learn more about Strings in Python here.

f-strings

name = "Subhankar"
age = 25
print(f"My name is {name} and I am {age} years old.")

This code will print the same string as the previous section, but using an f-string. The values within the curly braces are evaluated and incorporated into the string.

Using the ‘%‘ operator

name = "Subhankar"
age = 25
print("My name is %s and I am %d years old." % (name, age))

This code will also print the same string as before, but using the ‘%‘ operator. The ‘%s‘ placeholder is used for strings, and ‘%d‘ is used for integers.

‘format()’ method

name = "Subhankar"
age = 25
print("My name is {} and I am {} years old.".format(name, age))

Again, this code will also print the same string as before, but using the ‘format()‘ method. The placeholders in curly braces are replaced with the variables passed as arguments to the ‘format()‘ method.

Escape Sequence

In Python, you can use an escape character or backslash(‘\‘) to avoid the illegal characters in a string. Let’s see an example:

print('It's My first Python Program')

The above code will raise syntax errors due to the presence of a single quotation (‘) mark within the paragraph text.

The simple way to solve the above issue is to add an escape character (‘\‘) before any unauthorized characters, as demonstrated in the following example:

print('It\'s My first Python Program')

Here’s another example of printing illegal strings. Note that “hosts” is an unacceptable format within the string. However, by adding the ‘\‘ character beforehand, this issue can be resolved.

print("Every Operating System has a \"hosts\" file.")

Printing Raw Strings

Raw strings are special strings in programming that ignore backslashes (‘\‘). This is useful when you want to include backslashes in your string without having them interpreted as escape sequences.

For example, the following string would print a newline character:

text = "This is a string with a newline \n"
print(text)

This can be avoided using a raw string:

text = r"This is a string with a backslash \n"
print(text)

This would print: “This is a string with a backslash \n“.

Printing New Lines

When we write a program, it is often necessary to print multiple new lines. In Python, printing new lines is accomplished by using the ‘\n’ character. Take an example:

print("1. First Line\n2. Second Line\n3. Third Line")

The output would be:

1. First Line
2. Second Line
3. Third Line

Exercises

Try some exercises for practicing the Python ‘print()’ function:

  • Print the string “Hello, World!” to the console.
  • Print a string that includes double quotes, single quotes, and a backslash.
  • Print the result of a mathematical expression, such as 2 + 2 or 5 * 7.
  • Print a multi-line string using triple quotes.
  • Print a formatted string that includes variables, such as “My name is [name] and I am [age] years old.”
Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! I’m Subhankar Rakshit, the brains behind PySeek. I’m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles: 147