🌐
W3Schools
w3schools.com › python › python_match.asp
Python Match
The match statement is used to perform different actions based on different conditions. Instead of writing many if..else statements, you can use the match statement. The match statement selects one of many code blocks to be executed. match ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-match-case-statement
Python Match Case Statement - GeeksforGeeks
December 11, 2025 - In this example, the function check_number(x) uses a match-case statement to compare the value of x to the constants 10 and 20. If x equals 10, it prints "It's 10". If x equals 20, it prints "It's 20". If neither condition is met, the wildcard _ matches any value, leading to the message "It's neither 10 nor 20". Let's take a look at python match case statement in detail:
🌐
Tutorialspoint
tutorialspoint.com › home › python › python match case statement
Python Match Case Statement
February 21, 2009 - A Python match-case statement takes an expression and compares its value to successive patterns given as one or more case blocks. Only the first pattern that matches gets executed.
🌐
LearnPython.com
learnpython.com › blog › python-match-case-statement
How to Use a match case Statement in Python 3.10 | LearnPython.com
Check out the official Python downloads page for access to the most recent versions. If you want more detailed information on match case statements, there are three new Python Enhancement Proposals (PEPs). A good place to start is PEP 636, which is a tutorial for pattern matching.
🌐
Python Pool
pythonpool.com › home › blog › match case python: new addition to the language
Match Case Python: New Addition to the Language - Python Pool
January 9, 2022 - To implement switch-case like characteristics and if-else functionalities, we use a match case in python. A match statement will compare a given variable’s value to different shapes, also referred to as the pattern.
🌐
Programiz
programiz.com › python-programming › match-case
Python match…case Statement
The match…case statement allows us to execute different actions based on the value of an expression. In this tutorial, you will learn how to use the Python match…case with the help of examples.
🌐
W3Schools
w3schools.com › python › ref_keyword_case.asp
Python case Keyword
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... command = "start" match command: case "start": print("Starting...") case "stop": print("Stopping...") case _: print("Unknown command") Try it Yourself »
🌐
Datamentor
datamentor.io › python › match-case
Python match...case Statement (With Examples)
Python also lets us use or statements in the cases. For example, number = 2 match number: case 2 | 3: print('Small') case 4 | 5 | 6: print('Medium') case 9 | 10: print('Large') # Output: Small
🌐
Mimo
mimo.org › glossary › python › match-statement
Python Match Statement: A Versatile Switch-Case in Python
Start your coding journey with Python. Learn basics, data types, control flow, and more ... match subject: case pattern_1: # Action for pattern 1 case pattern_2: # Action for pattern 2 case _: # Default action if no other case matches
Find elsewhere
🌐
W3Schools
w3schools.com › python › python_challenges_match.asp
Python Match Code Challenge
Python If Python Elif Python Else Shorthand If Logical Operators Nested If Pass Statement Code Challenge Python Match
🌐
Python
peps.python.org › pep-0636
PEP 636 – Structural Pattern Matching: Tutorial | peps.python.org
This is called matching · It will bind some names in the pattern to component elements of your subject. In this case, if the list has two elements, it will bind action = subject[0] and obj = subject[1].
🌐
Readthedocs
pc-python.readthedocs.io › en › latest › python_advanced › match_case.html
6. Match - Case — PC-Python
Match case can be used to replace lengthy if-elif blocks, making the alternatives more readable. An example of a simple if-elif block is below. age_flag = False if age_flag == True: print("Entry permitted") elif age_flag == False: print("No entry until you reach 13 years of age.") In the simple pattern below, the subject is checked against each case pattern, in order, till a match is found.
🌐
Python.org
discuss.python.org › python help
Match/case syntax - Python Help - Discussions on Python.org
October 24, 2022 - In the dark ages when I used to code in vbScript I found this form of the select/case to be clearer than repeated if/else if blocks. I there a form of the new match/case in Python that accomplishes the same thing? I am aware that there are easier ways of coding this particular example but the beauty of this form is that the conditions in the case clauses do not have to be related. select case True case distance
Top answer
1 of 2
73

Rather than match type(v), match v directly:

values = [
    1,
    "hello",
    True,
]

for v in values:
    match v:
        case str():
            print("It is a string!")
        case bool():
            print("It is a boolean!")
        case int():
            print("It is an integer!")
        case _:
            print(f"It is a {type(v)}!")

Note that I've swapped the order of bool() and int() here, so that True being an instance of int doesn't cause issues.

This is a class pattern match.

2 of 2
11

You can match directly against the type of v, but you need a value pattern to refer to the types to match, as a "dotless" name is a capture pattern that matches any value. For example,

import builtins


values = [
    1,
    "hello",
    True
]

# Caveat: this will continue to work even if someone
# rebinds the built-in, but not, for example, if builtins.str
# itself is rebound.
for v in values:
    match type(v):
        case builtins.str:
            print("It is a string!")
        case builtins.int:
            print("It is an integer!")
        case builtins.bool:
            print("It is a boolean!")
        case _:
            print(f"It is a {type(v)}!")

Note that a value pattern must be a dotted name; it's not an arbitrary expression that can evaluate to a specific value.

(Whether you really want to match against the actual type of a value, or really want to determine if a value is an instance of a given type, is another matter. In the latter case, an if-elif statement is needed.

if isinstance(v, bool):
    print("It is a boolean!")
elif isinstance(v, int):
    print("It is an int!")
elif isinstance(v, str):
    print("It is a string!")
else:
    print(f"It is a {type(v)}!")

There is no pattern that lets you use the result of calling isinstance as the case to match against. )

🌐
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
Python has a built-in package called re, which can be used to work with Regular Expressions. ... You can add flags to the pattern when using regular expressions. A special sequence is a \ followed by one of the characters in the list below, and has a special meaning: A set is a set of characters inside a pair of square brackets [] with a special meaning: The findall() function returns a list containing all matches.
🌐
W3Schools
w3schools.com › python › gloss_python_regex_match.asp
Python RegEx Match Object
The regular expression looks for any words that starts with an upper case "S": import re txt = "The rain in Spain" x = re.search(r"\bS\w+", txt) print(x.group()) Try it Yourself » · Note: If there is no match, the value None will be returned, ...
🌐
Towards Data Science
towardsdatascience.com › home › latest › the match-case in python 3.10 is not that simple
The Match-Case In Python 3.10 Is Not That Simple | Towards Data Science
January 21, 2025 - If it doesn’t match one of the given 3 "times", the default case will be executed. Why there is a question mark there? I intentionally added it because I want to emphasise the solution. Generally, it won’t be easy to match one of the sub-patterns and then reference which pattern it matched exactly. However, we can have this "reference" in Python.
🌐
Gui Commits
guicommits.com › python-match-case-examples
Python Match Case Examples 🐍🕹️
September 7, 2022 - Python 3.10 has the match case which is Structural Pattern Matching. I'm going to show you what it can do with examples!
🌐
datagy
datagy.io › home › python posts › python switch (match-case) statements: complete guide
Python Switch (Match-Case) Statements: Complete Guide • datagy
February 23, 2022 - In this tutorial, you’ll learn how to use Python to create a switch-case statement. Prior to Python version 3.10, Python did not have an official switch-case statement. In order to accomplish this, you had a number of different options, such as if-else statements and dictionaries.