New Batch Starts on 16th Oct.
Module: Network Fundamentals (L2 L3 Protocol Testing)
Timing: 07:00 AM to 08:00 AM IST (Weekdays)
Sign Up Now !

Chapter 4: Mastering the Print Function in Python

The print function is one of the most essential tools in Python, allowing you to display information and debug your code effectively. In this chapter, we’ll learn the basics of print statements and explore how to print with descriptive text, use formatted string literals (F-strings), and work with format specifiers. You’ll also learn how to handle quotes in strings, concatenate multiple strings, and use the end and sep arguments to control output. By the end of this chapter, you’ll have a solid grasp of how to print output in Python with precision and flexibility


Table of Contents


Understanding and Using the Python Print Function

In this chapter, we’ll cover everything you need to know about Python’s print function. Let’s start with the syntax of a few simple print statements.

Simple Print Statements

You can print integers and floating-point numbers as they are. However, remember to use quotes when you are printing strings.

 For example, print ( 10 ) will work, but print ( hello ) will throw an error. The correct statement is print ( “hello” )

Okay… now start with these following simple print statements and try to understand how Python displays the output.

Note that parentheses, quotes and commas are part of the syntax and are not displayed in the output.

Try the code yourself:


You can print variables using the “print” function. Note that variable names should not be enclosed in quotes.


How to Print with Descriptive Text

You can use strings along with your variables if you want to print a description. For example, check the code below which mixes both strings and variables in one print statement.

Try to figure out which are the strings and which are the variables used in the above print statement.

Try the code yourself:


There are a few more ways to get the same output as the previous code. One option we have is f-strings, and the other is placeholders like %d, %s, %x, etc., known as format specifiers. Let’s check f-strings first.

Formatted String Literals or F-strings

Python 3.6 introduced f-strings which provide a way to embed expressions inside string literals, using curly braces ` { } `

Try to code yourself:


Using Format Specifiers

In this method, we can use some placeholders in the string and replace those with values of the variables. `%d`, `%s` are the two commonly used placeholders. `%d` is used for integers and `%s` for strings.

Try the code yourself:


Using String Concatenation in the Print Function

You can concatenate strings using the `+` operator.

For example, `”Hello” + “World”` gives `”HelloWorld”`. You can use these types of expressions directly inside the print statement.

Note that concatenation with `+` only works when you use the same data types on both sides of `+`. Attempting to concatenate a string with a non-string type will result in an error.

For example:


Handling Quotes in Strings

You may encounter situations where you need to include quotes within your strings. You can use a backslash ( `\` ) before quotes to avoid the special meaning of quotes, and this will display the quotes.

For example:

In Python, you can use single quotes or double quotes to make a string. If you have used single quotes at the beginning and at the end, you can use double quotes inside the string to display it.

For example:

 Try the code yourself:

But using the same type of quote for both the string and the embedded quote will result in a syntax error.


What is the `end` Argument in the Print Function?

By default, the `print` function ends with a newline character ( `\n` ). You can check this by using two print statements.

Try the two print commands given below:

We are getting the above output because Python adds a newline `\n` at the end of each print statement. The first print ( “hello” ) will get converted to print( “hello\n” ), so after printing the word “hello” a new line is added. The second print statement is displayed on the second line.

We can change this behavior by using the `end` argument.

For example:

In this example, the first `print` statement ends with a comma instead of a newline, so the second `print` statement continues on the same line.

You can use any string for the `end` argument.

For example:


What is the `sep` Argument in the Print Function?

You would have noticed that a space is added between the arguments in the print function.

For example,

In the above code `sep` argument is used to specify which character has to be used between the arguments.


Next >>> Chapter 5: Python Conditional Statements: if, else, and elif
Previous >>> Chapter 3: Working with Variables in Python


We’d love to hear your feedback and suggestions about this article. Feel free to reach out to us using the WhatsApp number below.

Sajith Achipra has been a trainer and testing consultant at Zframez Technologies since 2009. With 15+ years of experience, he specializes in networking, Python, development, and testing. He conducts online courses to help students and professionals enhance their skills. You can reach him on WhatsApp at +91 8884 884 844 for your training and testing requirements.