Python Input and Output: print(), input(), and Formatting
Every program needs a way to talk to the outside world. A weather app shows temperatures. A game displays your score. A calculator prints answers. All of these use output.
In Python, print() is your main tool for showing information to the user. It seems simple at first, but it has powerful options that let you control exactly how your output looks.
In this tutorial, you'll learn advanced print() techniques, formatting tricks for clean output, and how input() works in regular Python. By the end, you'll be able to create polished, professional-looking output.
How Does print() Work with Multiple Values?
You already know print("Hello") displays text. But print() can handle multiple values at once. Just separate them with commas, and Python prints them all with spaces in between.
Think of print() like a waiter reading off your order. You hand them several items, and they announce each one with a pause between them.
Notice something helpful: Python automatically adds a space between each value. You also don't need to convert numbers to strings. The print() function handles that for you.
What Does the sep Parameter Do in print()?
By default, print() puts a space between each value. The sep parameter lets you change that separator to anything you want. It stands for "separator."
Imagine you're stringing beads on a necklace. The sep is the knot you tie between each bead. You choose what goes between the items.
The sep parameter is perfect for building formatted output like dates, file paths, or CSV data without doing string concatenation yourself.
What Does the end Parameter Do in print()?
Every time print() finishes, it adds a newline character at the end. That's why each print() call starts on a new line. The end parameter lets you change what comes after the output.
Think of end like the punctuation at the end of a sentence. By default, Python hits "Enter" after each print. You can change that to a space, a comma, or nothing at all.
How Do You Combine sep and end Together?
You can use sep and end in the same print() call. This gives you full control over both what goes between values and what comes at the very end.
How Do You Create Aligned and Formatted Output?
Real programs need output that's easy to read. Tables, receipts, and reports all need columns that line up neatly. Python gives you several ways to achieve this.
The simplest approach uses the .ljust(), .rjust(), and .center() string methods. These pad a string with spaces to reach a target width.
For more control, use f-strings with format specifiers. Inside the curly braces, you can set the width and alignment of each value.
How Does the input() Function Work in Python?
Output is only half the story. Most programs also need to receive information from the user. In Python, the input() function pauses the program and waits for the user to type something.
Think of input() like a form field on a website. The program displays a prompt, the user types their answer, and the program stores that answer in a variable.
The input() function always returns a string, even if the user types a number. This is an important detail that catches many beginners off guard.
# input() always returns a string
# age = input("Enter your age: ")
# print(type(age)) # <class 'str'> — always a string!
# Simulating:
age = "25" # input() would return this as a string
print(type(age)) # <class 'str'>
print(age + 10) # TypeError! Can't add string + numberHow Do You Convert User Input to Numbers?
Since input() always returns a string, you need to convert it when you expect a number. Wrap input() with int() for whole numbers or float() for decimals.
What Are Common Output Patterns in Python Programs?
Professional programs use recognizable output patterns. Menus, headers, receipts, and progress indicators all follow predictable formats. Learning these patterns makes your programs feel polished.
Practice Exercises
Time to practice! These exercises focus on print() formatting since input() doesn't work in the browser. Try each one before checking hints.
Print the words "red", "green", and "blue" separated by - (space dash space) using the sep parameter of print().
Expected output:
red - green - blueUse a single print() call with the sep parameter.
Use a for loop to print the numbers 1 through 5 on the same line, separated by spaces. Add an empty print() at the end to finish the line.
Expected output:
1 2 3 4 5Use the end parameter inside the loop. Be careful not to add a trailing space after 5.
Create a multiplication table for numbers 1 through 3. Each product should be right-aligned and take up 4 characters of width. Print a header row and a separator line.
Expected output:
1 2 3
-----------
1 | 1 2 3
2 | 2 4 6
3 | 3 6 9Use f-string alignment ({value:>4}) to right-align numbers in 4-character columns.
Create a formatted receipt with the following items and prices. Use f-string alignment to left-align item names (20 characters wide) and right-align prices (8 characters wide, 2 decimal places). Include a header, separator, and total.
Items:
Expected output:
============================
TECH STORE
============================
Item Price
----------------------------
Laptop 999.99
Mouse 29.50
Keyboard 74.99
----------------------------
Total 1104.48
============================Summary: Python Input and Output
| Here's a quick reference of everything you learned: | ||
|---|---|---|
| --- | --- | --- |
| Print multiple values | print(a, b, c) | print("Hi", name) |
| Custom separator | sep="..." | print(1, 2, 3, sep="-") gives 1-2-3 |
| Custom ending | end="..." | print("Hi", end=" ") stays on same line |
| Left-align | f"{val:<width}" | f"{name:<20}" |
| Right-align | f"{val:>width}" | f"{price:>8.2f}" |
| Center | f"{val:^width}" | f"{title:^30}" |
| User input | input("prompt") | name = input("Name: ") |
| Convert input | int(input()) | age = int(input("Age: ")) |
Key takeaway: print() is far more powerful than just displaying text. With sep, end, and f-string formatting, you can create clean, professional output. The input() function works in regular Python terminals but not in browser-based environments.
What's Next?
Now that you can create polished output, you're ready to control the flow of your programs. In the next tutorials, you'll learn about if statements and loops -- the tools that let your programs make decisions and repeat actions.