Study Guide: Input, Output & String Formatting

Module 3 • Introduction to Python

1. The print() Function

Key Concept: print() is Python's primary way to display output. It can accept multiple values separated by commas and has two important parameters: sep and end.

Multiple Arguments

When you pass multiple values separated by commas, Python automatically inserts a space between them and converts non-string values to strings:

print("Name:", name, "Age:", age)
# Automatic spaces, automatic str() conversion

sep and end Parameters

print("a", "b", "c", sep="-")     # a-b-c
print("Hi", end=" "); print("there")  # Hi there

2. The input() Function

Key Concept: input() pauses the program, shows a prompt, and returns whatever the user types as a string. It ALWAYS returns a string, even if the user types a number.
Most Common Mistake: Forgetting to convert input() to a number before doing math. age = input("Age: ") stores a string, not a number. age + 1 will cause a TypeError. Fix: age = int(input("Age: ")).

The Standard Input Pattern

# For strings (no conversion needed)
name = input("Your name: ")

# For integers
age = int(input("Your age: "))

# For decimals
price = float(input("Price: "))

3. f-strings

Key Concept: f-strings (formatted string literals) are strings prefixed with f that let you embed Python expressions inside {}. They are the modern, preferred way to format strings in Python.

Basic Usage

name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")

Expressions in f-strings

You can put any Python expression inside the curly braces:

print(f"{x} + {y} = {x + y}")
print(f"Upper: {name.upper()}")
print(f"Length: {len(name)}")

Number Formatting

Remember: The % format specifier multiplies by 100 automatically. So 0.75 becomes 75%, not 0.75%.

Text Alignment

4. String Concatenation and Repetition

Key Concept: Use + to join strings (no automatic spaces) and * to repeat strings. Remember that concatenation only works between strings -- convert numbers with str() first.

When to Use Each Approach

5. The .format() Method

The older way to format strings. Know it for reading existing code, but prefer f-strings:

"Hello, {}!".format(name)
"Pi: {:.2f}".format(3.14159)

6. Review Questions

Q1: What does input() always return?

A: A string (str). Always, even if the user types a number.

Q2: What is the output of print("x", "y", "z", sep="")?

A: xyz -- empty string separator means no space between values.

Q3: What does f"{9.99:.0f}" produce?

A: 10 -- zero decimal places, so 9.99 rounds to 10.

Q4: How do you display the number 0.42 as a percentage with no decimal places?

A: f"{0.42:.0%}" produces 42%

Q5: What is wrong with "Total: " + 99.99?

A: TypeError -- you cannot concatenate a string with a float. Fix: "Total: " + str(99.99) or better yet f"Total: {99.99}"

Q6: What does print("Hi", end="!!! ") followed by print("Bye") output?

A: Hi!!! Bye -- the end parameter replaces the newline with "!!! ".

7. Key Terms Summary