Match Statements in Python

From the Python cheat sheet ยท Control Flow ยท verified Jul 2026

Match Statements

Structural pattern matching (Python 3.10+).

python
# Basic match
match command:
    case "quit":
        exit()
    case "hello":
        print("Hi!")
    case _:
        print("Unknown")
๐Ÿ’ก match/case is structural pattern matching, not just a switch statement
โšก Use | to match multiple values in one case (e.g., 500 | 502 | 503)
๐Ÿ“Œ The _ wildcard matches anything โ€” use it as a default/fallback case
๐ŸŸข Add a guard with "if" after the pattern for conditional matching
matchpattern-matchingcontrol-flow

More Python tasks

Back to the full Python cheat sheet