Skip to main content

Python Input and Output: print(), input(), and Formatting

Beginner15 min4 exercises90 XP
0/4 exercises

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.

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.

Printing multiple values with commas
Loading editor...

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.


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.

Using sep to control what goes between values
Loading editor...

The sep parameter is perfect for building formatted output like dates, file paths, or CSV data without doing string concatenation yourself.

Practical uses of sep
Loading editor...

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.

Using end to control line breaks
Loading editor...
Using end in a loop
Loading editor...

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.

Using sep and end together
Loading editor...

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.

Aligning text with ljust() and rjust()
Loading editor...

For more control, use f-strings with format specifiers. Inside the curly braces, you can set the width and alignment of each value.

f-string alignment specifiers
Loading editor...
A formatted multiplication table
Loading editor...

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.

How input() works (conceptual example)
Loading editor...

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
# 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 + number

How 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.

Converting strings to numbers
Loading editor...

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.

Pattern: Menu display
Loading editor...
Pattern: Formatted report
Loading editor...
Pattern: Simulated interactive program
Loading editor...

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.

Exercise 1: Print with a Custom Separator
Write Code

Print the words "red", "green", and "blue" separated by - (space dash space) using the sep parameter of print().

Expected output:

red - green - blue

Use a single print() call with the sep parameter.

Loading editor...
Exercise 2: Print Items on the Same Line
Write Code

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 5

Use the end parameter inside the loop. Be careful not to add a trailing space after 5.

Loading editor...
Exercise 3: Create a Multiplication Table
Write Code

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   9

Use f-string alignment ({value:>4}) to right-align numbers in 4-character columns.

Loading editor...
Exercise 4: Print a Formatted Receipt
Write Code

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:

  • Laptop: 999.99
  • Mouse: 29.50
  • Keyboard: 74.99
  • Expected output:

    ============================
          TECH STORE
    ============================
    Item                   Price
    ----------------------------
    Laptop               999.99
    Mouse                 29.50
    Keyboard              74.99
    ----------------------------
    Total               1104.48
    ============================
    Loading editor...

    Summary: Python Input and Output

    Here's a quick reference of everything you learned:
    ---------
    Print multiple valuesprint(a, b, c)print("Hi", name)
    Custom separatorsep="..."print(1, 2, 3, sep="-") gives 1-2-3
    Custom endingend="..."print("Hi", end=" ") stays on same line
    Left-alignf"{val:<width}"f"{name:<20}"
    Right-alignf"{val:>width}"f"{price:>8.2f}"
    Centerf"{val:^width}"f"{title:^30}"
    User inputinput("prompt")name = input("Name: ")
    Convert inputint(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.