Quick Reference: Input, Output & String Formatting

Module 3 • Introduction to Python

print() Function

# Basic printing
print("Hello, World!")
print(42)
print(name)

# Multiple values (auto-spaces)
print("Name:", name, "Age:", age)

# Custom separator
print("a", "b", "c", sep="-")   # a-b-c
print(3, 15, 2025, sep="/")     # 3/15/2025

# Custom ending
print("Hi", end=" ")
print("there")                   # Hi there

# Blank line
print()

input() Function

# Get text input
name = input("Your name: ")

# Get integer input
age = int(input("Your age: "))

# Get float input
price = float(input("Price: $"))

# Two-step approach
text = input("Enter number: ")
number = int(text)
input() ALWAYS returns a string. Convert with int() or float() for math.

String Concatenation & Repetition

# Concatenation (+)
full = first + " " + last
msg = "Age: " + str(25)  # must convert!

# Repetition (*)
line = "-" * 30
cheer = "Go! " * 3     # "Go! Go! Go! "

# Building with +=
result = ""
result += "Line 1\n"
result += "Line 2\n"

f-strings (Recommended)

# Basic f-string
print(f"Hello, {name}!")
print(f"{x} + {y} = {x + y}")

# Expressions inside {}
print(f"Upper: {name.upper()}")
print(f"Length: {len(name)}")

Number Formatting

FormatExampleResult
:.2ff"{3.14159:.2f}"3.14
:.0ff"{3.7:.0f}"4
:,f"{1000000:,}"1,000,000
:,.2ff"{1234.5:,.2f}"1,234.50
:.1%f"{0.85:.1%}"85.0%
:.0%f"{0.85:.0%}"85%

Text Alignment

# Left align (width 15)
f"{'Hello':<15}"   # "Hello          "

# Right align (width 15)
f"{'Hello':>15}"   # "          Hello"

# Center align (width 15)
f"{'Hello':^15}"   # "     Hello     "

# Practical table:
f"{'Item':<15} {'Price':>8}"
f"{'Apples':<15} {'$1.50':>8}"

.format() Method (Older Style)

# Positional
"Hi, {}!".format(name)

# Numbered
"{0} is {1}".format(name, age)

# Named
"{n} is {a}".format(n=name, a=age)

# With formatting
"Pi: {:.2f}".format(3.14159)
Use f-strings for new code. Know .format() for reading older code.

When to Use What

MethodBest For
f-stringsMixing text + variables (default choice)
Commas in print()Quick output, auto-spaces
Concatenation (+)Building string variables
Repetition (*)Visual patterns, borders