Lesson 01 · Beginner · 18 min

Python Print and Strings

Make Python show text, combine values, and format messages you can actually read.

print and sepquotes and escapesf-string formatting

On her first morning, Maya opens her aunt's coffee cart at seven, and the first customer wants a receipt, not a conversation. She has a name, a price, and a few lines of code to turn them into something readable at a bus stop. Which part of print() decides how it looks?

Every Python program starts by producing a result you can see. print() is the simplest way to inspect a value, test an idea, and show output to a user. In this lesson you will use strings, separators, and f-strings to build clear messages. You will also format a multi-column receipt and inspect hidden whitespace.

print() is a function

Call print() with one or more values. Python converts each value to text and writes it to the screen. By default, values are separated by a space and the line ends with a newline. You can change both behaviours with sep and end.

print("Latte", 4.5)
print("Latte", 4.5, sep=": ")
print("Loading", end="...")
print("done")

Separators and line endings

Strings need quotes

A string is text inside single or double quotes. Use the same quote at both ends. Triple quotes let a string span several lines. If the text contains an apostrophe, wrap it in double quotes or escape the apostrophe.

name = "Maya"
message = 'It\'s Python'
print(name)
print(message)

Creating strings

f-strings insert values

Put an f before the opening quote and place expressions inside curly braces. This is the clearest way to combine text and variables. Formatting options such as .2f keep money values readable.

name = "Maya"
price = 4.5
print(f"Hello, {name}!")
print(f"Total: ${price:.2f}")

Readable formatted output

Format a receipt line

Real output is usually a table, not a single value. Format specs set a minimum width and align columns: < left, > right, and .2f fixes two decimal places. The widths do not have to be perfect, but keeping them consistent makes a receipt readable.

name = "Latte"
qty = 2
price = 4.5
line_total = qty * price
print(f"{name:<8} {qty:>2} x {price:>5.2f} = {line_total:>6.2f}")
print(f"{'TOTAL':<8} {'':>2}   {'':>5}   {line_total:>6.2f}")

Width, alignment, and fixed decimals

Inspect output instead of guessing

print() hides the difference between an empty string, a space, and a newline. repr() shows those characters, and len() counts them. When output looks wrong, inspect the raw value first; guessing at formatting is slower than checking it.

message = "  Latte  \n"
print(repr(message))
print(len(message))
print(repr(message.strip()))
print(message.strip().split())

repr() exposes hidden characters

Return a value, do not just print it

print() writes text and returns None; return hands a value back to the caller. That difference matters as soon as a function is used twice. A function that prints can only be read by a human, while a function that returns can be tested, joined, or written to JSON. If you see None where you expected text, check whether the function printed instead of returning.

def label(name):
    return f"Item: {name}"

text = label("Latte")
print(text)
print(label("Tea"), end=" | ")
print(len(text))

A returned string can be reused

Key points

  • print() accepts multiple values and adds a newline by default.
  • sep controls the text between values; end controls what follows the line.
  • Use matching quotes for strings, or triple quotes for multiline text.
  • f-strings insert variables and expressions directly into text.
  • Format specs align columns; repr() exposes whitespace that print() hides.
  • print() is for people; return is for the rest of the program.

Common mistakes

  • Forgetting the closing quote, or mixing single and double quotes.
  • Using + to join a string and a number without converting the number first.
  • Putting a variable name in an f-string without curly braces, so it prints literally.
  • Treating repr() as another print(): it shows quotes and escape characters too.
  • Calling a function that only prints and expecting a value back.
Your turn

Print a two-line receipt for two items, with a dash separator between the item name and price.

Try it yourself

Change the values, predict the output, then run the code. The Python runtime is downloaded the first time you press Run, so the first run can take a few seconds.

In-browser PythonRuns locally · no account
First run downloads Pyodide from a CDN. Use Run code after editing.

Practice this lesson

12 questions with immediate explanations.

See all 12 answersStart practice
Variables & types →