Pylerno
English

Pattern matching

Use match for a small command vocabulary and understand its fallback.

On this page

The concept

A match statement compares a subject with patterns. This lesson uses literal strings, where the intent is a readable dispatch table for commands. case "help" | "?" accepts either literal. The wildcard _ handles anything not matched earlier.

Patterns are not arbitrary boolean expressions. A bare name in a pattern can capture a value rather than compare it with an existing variable. Keep quotation marks around literal command names. Ordinary if statements remain a good choice for ranges and more general conditions; match is an additional tool, not a required replacement.

Worked example

python
command = "help"
match command:
    case "help" | "?":
        print("Commands: help, run, quit")
    case "run":
        print("Running")
    case "quit":
        print("Goodbye")
    case _:
        print("Unknown command")

Trace the result

  1. The subject is the current command value.
  2. The first case accepts either help or a question mark and displays the available commands.
  3. An unknown command reaches the wildcard case. Change the subject and predict which case runs.
text
Commands: help, run, quit
Line-by-line walkthrough

Line 1: command = "help"

Evaluate 'help', then bind or update command with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Lines 2–10: match command:

Compare command against the cases in order and execute the first matching case. The wildcard case is the fallback.

Case 'help' | '?': this suite runs only when its pattern matches.

Line 4: print("Commands: help, run, quit")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Case 'run': this suite runs only when its pattern matches.

Line 6: print("Running")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Case 'quit': this suite runs only when its pattern matches.

Line 8: print("Goodbye")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Case _: this suite runs only when its pattern matches.

Line 10: print("Unknown command")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

Do not remove quotes from a literal command and expect equality testing. A name pattern has different semantics.

Check your understanding

What does this program print?

python
match 2:
    case 1: print("one")
    case _: print("other")
Choose one answer

Search documentation

Share this page