Please enable JavaScript to use CodeHS

Printing in Python

Learn how to print information in Python out onto a console.

By Evelyn Hunter

Often when creating programs, we want to display information or data to users so that they are able to understand and use that information. In Python, the easiest way to display information to a user is by using a print() statement. print is used to display a variety of data types - like Strings, integers, and objects - to the terminal console. To use print, we can simply include the value or variable we’d like displayed to the console as a parameter in the print function:

name = karel
print("Welcome to CodeHS")   #prints Welcome to CodeHS
print(name)                  #prints karel
Plain text

In the program following, try using print to print your name and hometown to the console!

print can also take multiple values in the same call to print, and separate them using a default separator. The default separator spaces each parameter given to the print statement by a single space:

name = "Karel"
hometown = "San Francisco, California"
print(name, hometown)             #prints Karel San Francisco, California
Plain text

We can override the default separator by adding the keyword sep as the last parameter in our print statement, and set it equal to the value that we’d like to separate each value with:

name = "Karel"
hometown = "San Francisco, California"
print(name, hometown, sep = " of ")             #prints Karel of San Francisco, California
Plain text

In this case, the two variables name and hometown are being separated by the value " of ". If we added a third value to the parameter list, it would also be separated with the same value:

name = "Karel"
hometown = "San Francisco, California"
country = "The United States"
print(name, hometown, country, sep = " of ")             #prints Karel of San Francisco, California of The United States
Plain text

See if you can correctly use the sep keyword in the following exercise:

An exercise instructor wants to print out their workout instructions so their users can follow along. They find that they are repeating the phrase “1 and 2 and 3 and 4” over and over again as they give their instructions. Using only the print statement and the word “and” only one time, try to print this phrase to the Python console.

As demonstrated in the first exercise, print statements default to printing each statement on a new line in the console. We can override this behavior by including the end keyword as a value in the parameter list:

name = "Karel"
hometown = "San Francisco, California"
print(name, end = " of ")
print(hometown)             #prints Karel of San Francisco, California
Plain text

Think you’ve mastered the print statement? See how well you do on this quiz:

  1. Incorrect Correct No Answer was selected Invalid Answer

    Which Python code segment will display “Hello, world!” on the screen?

  2. Incorrect Correct No Answer was selected Invalid Answer

    What does the following Python code display?

    print("Hello")
    print("World")