How to Print in Python
What you’ll build or solve
You’ll print text and values using print(), combine multiple values in one line, and control formatting like separators and line endings.
When this approach works best
This approach works best when you need to:
Learn Python on Mimo
- Debug code by showing variable values while you work.
- Display results in a small script, like totals, status messages, or progress updates.
- Build a simple command-line program that guides someone through steps.
Avoid this approach when:
- You need long-term logs or different log levels. Use the
loggingmodule instead of manyprint()calls. - You need a graphical or web user interface.
print()is for terminal output.
Prerequisites
- Python 3 installed
- You know how to run Python code, either in a
.pyfile or an interactive shell - You understand basic variables like
name = "Sam"andcount = 3
Step-by-step instructions
1) Print a basic message
Use print() with a string.
Bash
print("Hello, world!")
print("Welcome to the program.")
2) Print variables and multiple values
Option A: Pass multiple arguments to print()
Bash
name="Lea"
score=42
print("Name:",name)
print("Score:",score)
print() automatically adds a space between arguments.
Option B: Use an f-string
Python
name="Lea"
score=42
print(f"Name:{name}")
print(f"Score:{score}")
With f-strings, you control the exact formatting inside the string.
What to look for:
Use f-strings when you want precise formatting. Use multiple arguments when you want simple spacing.
3) Print with a custom separator
Use the sep parameter to control what goes between values.
parts= ["2026","02","11"]
print(parts[0],parts[1],parts[2],sep="-")
Output:
2026-02-11
Another common case is CSV-style output:
name="Leah"
city="Boston"
print(name,city,sep=",")
4) Print without a newline
By default, print() ends with a newline. Use the end parameter to change that.
LUA
print("Loading",end="")
print(".",end="")
print(".")
Print items on one line in a loop:
fornin [1,2,3]:
print(n,end=" ")
print()# finish the line
What to look for:
If output runs together unexpectedly, add a final print() to end the line.
5) Print quotes, backslashes, and special characters
Use escape sequences for characters that would otherwise end the string.
Swift
print("She said:\"Hi\"")
print("Path: C:\\Users\\Lea")
print("First line\nSecond line")
For Windows paths, a raw string is often cleaner:
Python
print(r"C:\Users\Lea\Documents")
6) Print numbers with formatting
Format numbers with f-strings for readable output.
Round to two decimal places:
Python
price=19.995
print(f"Price:{price:.2f}")
Add thousands separators:
Python
views=1200000
print(f"Views:{views:,}")
Examples you can copy
1) Show a summary line from variables
product="coffee"
qty=3
unit_price=2.5
print(f"{qty} x{product} ={qty*unit_price:.2f} EUR")
2) Print a simple table-like output
Python
rows= [
("Lea",42),
("Omar",37),
("Kai",50),
]
print("Name | Score")
print("------------")
forname,scoreinrows:
print(f"{name} |{score}")
3) Print progress on one line
Python
steps=5
foriinrange(1,steps+1):
print(f"Step{i}/{steps}",end=" ")
print()
Common mistakes and how to fix them
Mistake 1: Forgetting quotes around text
You might write:
print(Hello)
Why it breaks:
Python treats Hello as a variable name, so you get a NameError.
Correct approach:
Bash
print("Hello")
Mistake 2: Trying to add strings and numbers with +
You might write:
Bash
age=34
print("Age: "+age)
Why it breaks:
You can’t concatenate a string and an integer directly.
Correct approaches:
Python
age=34
print("Age:",age)
print("Age: "+str(age))
print(f"Age:{age}")
Mistake 3: Unexpected newlines or missing spaces
You might write:
LUA
print("A","B",sep="")
print("Done",end="")
print("!")
Why it breaks:
sep="" removes spacing between arguments, and end="" removes the newline.
Correct approach:
LUA
print("A","B")
print("Done",end=" ")
print("!")
Troubleshooting
If you see NameError: name 'Hello' is not defined, put quotes around your text:
Bash
print("Hello")
If you see TypeError: can only concatenate str (not "int") to str, use print("x:", value) or an f-string instead of +.
If backslashes in a Windows path look wrong, use a raw string like r"C:\Users\Name" or escape them as \\.
If output appears on one long line, remove end="" or add a final print().
If you see literal \n instead of a newline, you might be printing a raw string. Use a normal string if you want escape sequences to work.
Quick recap
- Print text with
print("text"). - Print variables with
print("Label:", value)orprint(f"Label: {value}"). - Control separators with
sep="-"and line endings withend=" ". - Escape quotes and backslashes with
\"and\\, or use raw strings for paths. - Format numbers with f-strings like
{value:.2f}or{value:,}.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot