You can simply use the guard feature along with capture:

>>>my_str = "iwill/predict_something"
>>>match my_str:
...    case str(x) if 'predict' in x:
...        print("match!")
...    case _:
...        print("nah, dog")
...
match!
Answer from hyp3rg3om3tric on Stack Overflow
🌐
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:
🌐
Python.org
discuss.python.org › ideas
Partial string matches in structural pattern matching - Ideas - Discussions on Python.org
July 20, 2023 - Hi there, I’d love to be able to use partial string matches: match text: case "prefix_" + cmd: # checking prefix print("got", cmd) case "Hello " + last_name + ", " + first_name + "!": # more complex e…
🌐
Mimo
mimo.org › glossary › python › match-statement
Python Match Statement: A Versatile Switch-Case in Python
You can use the equivalent of the switch-case statement in Python to simplify complex conditionals. Instead of using multiple if-elif statements, you can use cases to check against. ... def http_status_code(status): match status: case 200: return "OK" case 404: return "Not Found" case 500: return "Server Error" case _: return "Unknown Status" The statement is also helpful for checking sequence patterns in user input. For example, you can match string patterns to ensure the input meets certain conditions.
🌐
W3Schools
w3schools.com › python › python_match.asp
Python Match
month = 5 day = 4 match day: case 1 | 2 | 3 | 4 | 5 if month == 4: print("A weekday in April") case 1 | 2 | 3 | 4 | 5 if month == 5: print("A weekday in May") case _: print("No match") Try it Yourself » ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Readthedocs
pc-python.readthedocs.io › en › latest › python_advanced › match_case.html
6. Match - Case — PC-Python
See: https://datagy.io/python-switch-case/ Match-case can be used to match all the simple data types: string, numbers, booleans, lists, tuples, sets, dictionaries. Match case can be used to replace lengthy if-elif blocks, making the alternatives more readable.
🌐
Python
peps.python.org › pep-0636
PEP 636 – Structural Pattern Matching: Tutorial | peps.python.org
The match statement will check patterns from top to bottom. If the pattern doesn’t match the subject, the next pattern will be tried. However, once the first matching pattern is found, the body of that case is executed, and all further cases are ignored.
🌐
LearnPython.com
learnpython.com › blog › python-match-case-statement
How to Use a match case Statement in Python 3.10 | LearnPython.com
This returns a list of strings. (By the way, if you are wondering what the difference between lists and arrays is, we explain it in this article.) The first case is matched when the value of command is 'show', for which the split() method returns the list ['show']. Then code to list all files in a particular directory gets executed.
Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › home › python › python match case statement
Python Match Case Statement
February 21, 2009 - With the release of Python 3.10, a pattern matching technique called match-case has been introduced, which is similar to the switch-case construct available in C/C++/Java etc. Its basic use is to compare a variable against one or more values.
🌐
Plain English Westminster
benhoyt.com › writings › python-pattern-matching
Structural pattern matching in Python 3.10
However, as the rationale PEP points out, it’s better thought of as a “generalized concept of iterable unpacking”. Many people have asked for a switch in Python over the years, though I can see why that has never been added. It just doesn’t provide enough value over a bunch of if ... elif statements to pay for itself. The new match ... case feature provides the basics of switch, plus the “structural” matching part – and some.
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. )

🌐
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!
🌐
Python Morsels
pythonmorsels.com › match-case-parsing-python
Appreciating Python's match-case by parsing Python code - Python Morsels
June 22, 2022 - def do_get_available_languages(parser, token): match token.split_contents(): case [name, "for", code "as" info]: return GetLanguageInfoNode(parser.compile_filter(code), info) case [name, *rest]: raise TemplateSyntaxError( f"'{name}' requires 'for string as variable' (got {rest!r})" ) Even if you don't understand how structural pattern matching works, that second block of code is likely easier to make guesses about at a glance. Just like with tuple unpacking, that match-case statement visually demonstrates the shape of our code. Python's match-case statement can even be used to match nested dictionary items.
🌐
The Teclado Blog
blog.teclado.com › python-match-case
Using "match...case" in Python 3.10
October 26, 2022 - Any cases below this case will never run because all cases will match with the underscore case. This is similar to the else keyword in an if...else. The _ case matches everything because Python recognizes _ as a valid variable name. So just like when we matched the case ["hello", name], the comparison expression will get binded to the _ name.
🌐
Sling Academy
slingacademy.com › article › python-match-case-statement
Python match/case statement (with examples) - Sling Academy
The match/case statement in Python is used to implement switch-case like characteristics and if-else functionalities. It was introduced in Python 3.10. The match statement compares a given variable’s value to different shapes, also referred to as patterns, until it fits into one.
🌐
Udacity
udacity.com › blog › 2021 › 10 › python-match-case-statement-example-alternatives.html
Python Match-Case Statement: Example & Alternatives | Udacity
September 27, 2022 - Once a matching case clause is found, the code inside that case clause is run. If there is no matching case clause for expression, the code under the default clause gets executed. Let’s look at how to implement this in Python 3.10, using the new match-case statement.
🌐
Reddit
reddit.com › r/learnpython › is it "right" to use the match/case statement rather than if/else when you just want to check certain conditions?
r/learnpython on Reddit: Is it "right" to use the match/case statement rather than if/else when you just want to check certain conditions?
December 11, 2024 -

I can't say I fully understand the match/case statement yet, but I know a part of it involves more than just simple pattern matching. I know that the expression in the match statement, for example, actually runs and creates a result, such as an object if it's a call to a class.

So I'm wondering, if I don't want to do all that and just want to use it as a cleaner version of if/else, is this considered Pythonic, or is it overkill?

For example:

if some_value == True and some_other == 1:
    do something
elif some_value == False and some_other == 1:
    do something else
etc etc etc

or:

match (some_value, some_other):
    case (True, 1):
        do something
    case (False, 1):
        do something else
    etc etc etc

First off, I *think* I'm getting the syntax correct! Second, I'm not actually creating any values with the expressions, so it feels like a glorified if/else construct, just cleaner looking.

Is this a valid use of match/case?

Thanks!

🌐
iO Flood
ioflood.com › blog › python-match-case
Python Case Matching: Your Ultimate Guide
January 24, 2024 - In this example, we’re using the re.match() function to match the pattern ‘Python’ at the start of the string ‘Python is fun’. The function returns a match object if the pattern is found, or None if it’s not.
🌐
Python.org
discuss.python.org › ideas
F-strings in match case - Ideas - Discussions on Python.org
June 30, 2023 - Allow f-strings to be evaluated in a match case setting match Foo: case f"Greater than {Bar}": print("Foo > Bar") This currently raises a SyntaxError: patterns may only match literals and attribute lookups