Please enable JavaScript to use CodeHS

Python Tutorial

Printing in Python

Every program needs a way to talk to you. In Python, that's the print() function - your program's own receipt printer for words, numbers, and anything else you want to show on the screen.

python ~15 min Beginner friendly
The Big Idea

What is print()?

When you write a program, you usually want it to show something - a message, a score, an answer. In Python, the easiest way to display information is the print() function. It takes whatever you put between the parentheses and writes it to the console (the text screen where your program's output appears).

print() can display all kinds of data: strings (text), integers (whole numbers), variables, and more. Put the value you want displayed inside the parentheses, like feeding a slip of paper into a printer:

main.py
name = "Karel"
print("Welcome to CodeHS")   # prints the text in quotes
print(name)                  # prints the value stored in name
Console
Welcome to CodeHS
Karel

Notice: each print() statement gets its own new line in the console, like a receipt printer feeding the paper forward after every item.

Know Your Ink

Strings, numbers, and variables

The quotes matter! Here's how Python decides what you mean:

  • Text in quotes is a string. print("hello") prints exactly the characters hello.
  • Numbers need no quotes. print(7 + 3) does the math first and prints 10. But print("7 + 3") is a string, so it prints the characters 7 + 3, no math done.
  • A name without quotes is treated as a variable. print(name) looks up what's stored in name and prints that value.
main.py
age = 13
print("age")     # a string: prints the word age
print(age)       # a variable: prints its value
print(7 + 3)     # math first, then print
print("7 + 3")   # just characters in quotes
Console
age
13
10
7 + 3

Classic mistake: writing print(hello) without quotes. Python thinks hello is a variable, can't find one with that name, and stops with NameError: name 'hello' is not defined. You'll try this yourself in the playground below.

Your Turn

Practice: introduce yourself

1

Name and Hometown

Open a Python editor (or a scrap of paper!) and write a program that uses print to print your name and your hometown to the console, each on its own line.

solution.py
# print your name to the console
print("Karel the Dog")

# print out your hometown!
print("San Francisco, California")
Console
Karel the Dog
San Francisco, California

Don't forget the quotes! Your name is text, so it needs to be a string. Without quotes, Python would go hunting for a variable called Karel.


Mix It Up

Combining things in one print

Option 1: commas

print can take multiple values in the same call, separated by commas. Python prints them all on one line and automatically puts a single space between each one (that space is called the default separator):

main.py
name = "Karel"
hometown = "San Francisco, California"
print(name, hometown)
Console
Karel San Francisco, California

Commas are friendly: you can mix strings and numbers freely, and Python handles the spacing for you.

Option 2: the + sign (concatenation)

You can also glue strings together with +. That's called concatenation. With + there's no automatic space, so you add your own. And there's one big rule: + only glues strings to strings. To mix in a number, wrap it in str() to convert it to a string first:

main.py
age = 13
print("Karel is", age, "years old")          # commas: mixing is fine
print("Karel is " + str(age) + " years old") # +: convert with str()
Console
Karel is 13 years old
Karel is 13 years old

Watch out: print("Karel is " + age) crashes with TypeError: can only concatenate str (not "int") to str, because age is a number. Either use commas or wrap it: str(age).

Interactive

print() playground

Time to run the printer yourself. Pick a style, type your own text, and watch the Python line get assembled and printed. Then flip the gotcha switch to see exactly how each classic mistake breaks:

print() playground
main.py - built live

        
Console

        


Tricky Characters

Quotes inside strings

What if the text you want to print contains a quote mark? If you write print("She said "hi""), Python thinks the string ends at the second " and gets confused. You have three ways out:

  • Switch quote styles. Python accepts 'single' or "double" quotes, so wrap the string in the kind you're not using inside.
  • Escape it. A backslash before a quote, \", tells Python "this is just a character, not the end of the string."

Click through the three options:

Escape the quotes
Console

        


Pro Formatting

Formatting tricks: sep, end, and \n

Change the separator with sep

Remember the automatic space between comma-separated values? You can replace it! Add the keyword sep as the last thing in your print statement and set it to whatever you want between the values:

main.py
name = "Karel"
hometown = "San Francisco, California"
country = "The United States"
print(name, hometown, sep=" of ")
print(name, hometown, country, sep=" of ")
Console
Karel of San Francisco, California
Karel of San Francisco, California of The United States

Every value in the list gets the same separator - add a third value and it gets " of " too.

Change the line ending with end

By default, every print ends by moving to a new line. The end keyword overrides that, so the next print continues right where this one left off:

main.py
name = "Karel"
hometown = "San Francisco, California"
print(name, end=" of ")
print(hometown)
Console
Karel of San Francisco, California

Here's end chaining four prints onto one line, then letting the last one start a fresh line:

main.py
print("1", end=" and ")
print("2", end=" and ")
print("3", end=" and ")
print("4")
print("Let's do it again!")
Console
1 and 2 and 3 and 4
Let's do it again!

Newlines anywhere with \n

One more trick: the special character \n means "new line" inside a string. One print statement can produce several lines:

main.py
print("roses are red\nviolets are blue")
Console
roses are red
violets are blue

Secret revealed: the default separator is sep=" " and the default ending is end="\n". Every print you've ever written was quietly using both!

Your Turn

Practice: workout coach

2

1 and 2 and 3 and 4

An exercise instructor wants to print out their workout instructions so their users can follow along. They find themselves repeating the phrase "1 and 2 and 3 and 4" over and over as they give instructions.

Using only one print statement, and typing the word and only one time, print this phrase to the console:

Expected output
1 and 2 and 3 and 4
solution.py
# use the sep keyword to print "1 and 2 and 3 and 4"
print(1, 2, 3, 4, sep=" and ")
Console
1 and 2 and 3 and 4

The separator " and " is placed between every pair of values, so one and in your code becomes three in the output. Way better than typing it three times!


Code

A printing example

Printing is one of the first things you learn. This program shows strings, numbers, and f-strings all being printed.


Check Yourself

Quick quiz

1. What does print("7 + 3") display in the console?

2. What does print("a", "b", "c", sep="-") print?

3. With age = 12, which line prints I am 12 without crashing?

Keep learning Python

Now your programs can talk. Next, teach them to remember things with variables, then to listen with user input - print, store, ask: that's the start of every interactive program.