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:

  • 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 logging module instead of many print() 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 .py file or an interactive shell
  • You understand basic variables like name = "Sam" and count = 3

Step-by-step instructions

1) Print a basic message

Use print() with a string.

print("Hello, world!")
print("Welcome to the program.")

2) Print variables and multiple values

Option A: Pass multiple arguments to print()

name="Lea"
score=42

print("Name:",name)
print("Score:",score)

print() automatically adds a space between arguments.

Option B: Use an f-string

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.

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.

print("She said:\"Hi\"")
print("Path: C:\\Users\\Lea")
print("First line\nSecond line")

For Windows paths, a raw string is often cleaner:

print(r"C:\Users\Lea\Documents")

6) Print numbers with formatting

Format numbers with f-strings for readable output.

Round to two decimal places:

price=19.995
print(f"Price:{price:.2f}")

Add thousands separators:

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

rows= [
    ("Lea",42),
    ("Omar",37),
    ("Kai",50),
]

print("Name | Score")
print("------------")

forname,scoreinrows:
print(f"{name} |{score}")

3) Print progress on one line

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:

print("Hello")

Mistake 2: Trying to add strings and numbers with +

You might write:

age=34
print("Age: "+age)

Why it breaks:

You can’t concatenate a string and an integer directly.

Correct approaches:

age=34

print("Age:",age)
print("Age: "+str(age))
print(f"Age:{age}")

Mistake 3: Unexpected newlines or missing spaces

You might write:

print("A","B",sep="")
print("Done",end="")
print("!")

Why it breaks:

sep="" removes spacing between arguments, and end="" removes the newline.

Correct approach:

print("A","B")
print("Done",end=" ")
print("!")

Troubleshooting

If you see NameError: name 'Hello' is not defined, put quotes around your text:

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) or print(f"Label: {value}").
  • Control separators with sep="-" and line endings with end=" ".
  • Escape quotes and backslashes with \" and \\, or use raw strings for paths.
  • Format numbers with f-strings like {value:.2f} or {value:,}.