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.
print ( 10 ) # Output : 10
print ( "Hello" ) # Output : Hello
print ( 10, 20 ) # Output : 10 20
print ( "Hello world" ) # Output : Hello world
print ( "Hello", 10 ) # Output : Hello 10
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.
a = 10
b = "Hello"
print ( a ) # Output : 10
print ( a, b ) # Output : 10 Hello
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.
name, age = "Peter", 20
print ( "Name is", name, "and age is", age )
# Output : Name is Peter and age is 20
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 ` { } `
a = 10
print ( f" a is { a } ") # Output : a is 10
#You can combine multiple variables in an f-string as well.
name, age = "Peter", 20
print (f" Name is { name } and age is { age } ") #Output : Name is Peter and age is 20
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.
name, age = "Peter", 20
print ( "Name is %s " %name) #Output : Name is Peter
print ( "Age is %d " %age) #Output : Age is 20
#Following example shows how to keep two placeholders in the same string.
print ( "Name is %s and age is %d “ %(name, age) ) #Output : Name is Peter and age is 20
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.
a, b = "Hello", "World"
print ( a + b ) # Output : HelloWorld
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:
print ( "Hello" + 10 ) # Will throw a TypeError
print ( "Hello" + "10" ) #Output : Hello10
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:
print("Interface \"EthernetO\" is up")
#Output : Interface "EthernetO" is up
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:
print ( 'Interface "EthernetO" is up' )
print (" Interface 'EthernetO' is up" )
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.
print ( "Interface "EthernetO" is up" )
#Output : SyntaxError: invalid syntax
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:
print("Hello")
print("World")
#You will get the following output:
Hello
World
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:
print ( "Hello", end = "," )
print ( "World" )
# Output:
Hello,World
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:
print("Hello", end="---")
print("World") # Output : Hello---World
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,
print ( "Learning", "Python", "Basics" ) #Output : Learning Python Basics
Print ( "Learning", "Python", "Basics", sep="-")
#Output: Learning-Python-Basics
In the above code `sep` argument is used to specify which character has to be used between the arguments.
We’d love to hear your feedback and suggestions about this article. Feel free to reach out to us using the WhatsApp number below.
About The Author:
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.