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:
name = "Karel" print("Welcome to CodeHS") # prints the text in quotes print(name) # prints the value stored in name
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.
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.
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
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.
Practice: introduce yourself
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.
# print your name to the console print("Karel the Dog") # print out your hometown! print("San Francisco, California")
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.
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):
name = "Karel" hometown = "San Francisco, California" print(name, hometown)
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:
age = 13 print("Karel is", age, "years old") # commas: mixing is fine print("Karel is " + str(age) + " years old") # +: convert with str()
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).
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:
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:
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:
name = "Karel" hometown = "San Francisco, California" country = "The United States" print(name, hometown, sep=" of ") print(name, hometown, country, sep=" of ")
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:
name = "Karel" hometown = "San Francisco, California" print(name, end=" of ") print(hometown)
Karel of San Francisco, California
Here's end chaining four prints onto one line, then letting the last one start a fresh line:
print("1", end=" and ") print("2", end=" and ") print("3", end=" and ") print("4") print("Let's do it again!")
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:
print("roses are red\nviolets are blue")
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!
Practice: workout coach
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:
1 and 2 and 3 and 4
# use the sep keyword to print "1 and 2 and 3 and 4" print(1, 2, 3, 4, sep=" and ")
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!
A printing example
Printing is one of the first things you learn. This program shows strings, numbers, and f-strings all being printed.
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.