Print Statements

Written by Ferhawn Shaheen (edited on 8/18/2021)

Introduction

Print statements are basic tools used for displaying output. Various data types (e.g. lists, strings, integers, etc.) can be displayed through the print statement. They are quite useful for both beginner and seasoned programmers. This lesson will discuss the basics of print statements along with some useful ways to format output.

Format

The following illustrates the general format of a print statement. Some of you may have even seen this before.

print(some_value)

# Output:
# some_value.

Examples

You can get creative with what you would like to output from a print statement. Here are some examples.

# This print statement will display the string pass into the print function.
print("I am the best string!")

# Output: 
I am the best string!


# In some cases, it is ideal to print the value of a variable with a print 
# statement.
name = "John"
print(name)

# Output:
John

# Math operations can even be displayed.
print(1+1)

# Output:
2

These examples are just a few of the many objects that can be displayed in Python.

.format() and \n

When working with print statements, one will often find that strings are displayed. In some cases, it is helpful to use the method .format(). This allows values, such as variables, to be displayed in specific parts of the string.

\n is a newline character, which reduces the number of print statements needed for multiple lines of text

Examples

The following example utilizes .format() to display the values of a variable in a string.

first_name = "Evan"
last_name = "Peterson"

print("Hello! My name is {} {} and I like space!".format(first_name, last_name))

# Output:
My name is Evan Peterson and I like space!

Looking at the example, you will notice that the curly brackets {} are used as placeholders to store the values we want to display. The method .format() follows the string and is still in the print statement, explaining the second parenthesis at the end. In the parentheses of .format(), we enter the values we would like to be present in the bracket placeholders.

It is also worth noting that the order you place your values in will correspond to which braces the value is placed in. first_name is first in the .format() parentheses, so it pairs with the first set of braces.

print("Here is a list of the winners:\nEvan\nFinn\nJoseph")

# Output:
Here is a list of the winners:
Evan
Finn
Joseph

As previously mentioned, the newline parameter is used to output multiline strings. This makes the code cleaner.

f-strings

In addition to .format(), it may be helpful to use f-strings instead. These are simply an alternative, and are illustrated in the following examples.

Examples

name = "Joseph"
print(f"{name}, get off the stage!")

# Output:
Joseph, get off the stage!

.format() versus f-strings

While the differences in syntax between the two are clear, it may be puzzling as to why both are considered. Why not use one instead of the other? It is true that both methods are similar, but it is often advised to use .format() if one has several values to place in a string. This would make it easier to keep track of what is being entered. On the other hand, f-strings are helpful for shorter print statements since they are a bit cleaner and easier to enter.

end and sep

The end and sep parameters to the print function are useful in cleaning up the output of a print statement. Unlike .format(), end and sep can be used on most data types as long as they are in the print statement.

end

The end parameter is used to combine multiple print statements. This is done by eliminating the newline parameter (\n) in a string. Since print statements in Python automatically append a new line, end helps prevent this. The purpose of end can be useful when you simply seek to combine what is printed in multiple statements.

Examples

# `end` combines these two statements. The extra space in the first statement
# is to ensure a space exists between the words "saying" and "that".
print("I am going to start by saying", end = " ")
print("that I want to combine these statements.", end = "")

# Output:
I am going to start by saying that I want to combine these statements.
# This illustrates that other data types, such as integers, apply to end.
print(1, 2, 3, end = " ")
print(4, 5, 6, end = "")

# Output:
1, 2, 3, 4, 5, 6

Notice how end is an argument, with it being separated by a comma. The value in the quotes (an empty space) is what is appended to the end of the string when it's printed. Without the empty space, the first set of code would output I am going to start by sayingthat I want to combine these statements.

The value in the quotes can essentially be whatever you want it to be.

sep

The sep parameter is used to separate multiple arguments in a print statement with a custom string. This custom value is what goes in the empty quotes (" "), just like end.

Examples

print(209, 547, 8990, sep = "-")

# Output:
209-547-8990

Notice the hyphen that takes its place in the quotes for sep. This allows the separate parts of a phone number to come together cleanly.

Also note that the value for sep is not appended to the end of the string, i.e. the output is not 209-547-8990-.

print(fshaheen, gmail.com, sep = "@")

# Output:
fshaheen@gmail.com

Similarly to the previous example, a character (@) is used to combine the arguments in the print statement (fshaheen and gmail.com).

Conclusion

This lesson covered the basics of a print statement along with some useful methods to organize and format the output of a print statement. As one learns more about programming, it will become far more apparent that print statements are in Python and other languages.

Last updated