Module 3 • Introduction to Python
# 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()
# 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.# 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"
# 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)}")
| Format | Example | Result |
|---|---|---|
:.2f | f"{3.14159:.2f}" | 3.14 |
:.0f | f"{3.7:.0f}" | 4 |
:, | f"{1000000:,}" | 1,000,000 |
:,.2f | f"{1234.5:,.2f}" | 1,234.50 |
:.1% | f"{0.85:.1%}" | 85.0% |
:.0% | f"{0.85:.0%}" | 85% |
# 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}"
# 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)
.format() for reading older code.| Method | Best For |
|---|---|
| f-strings | Mixing text + variables (default choice) |
| Commas in print() | Quick output, auto-spaces |
| Concatenation (+) | Building string variables |
| Repetition (*) | Visual patterns, borders |