You can define a default case in Python. For this you use a wild card (_). The following code demonstrates it:

x = "hello"
match x:
    case "hi":
        print(x)
    case "hey":
        print(x)
    case _:
        print("not matched")
Answer from Prince Hamza on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_match.asp
Python Match
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 ... 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 expression: case x: code block case y: code block case z: code block
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-match-case-statement
Python Match Case Statement - GeeksforGeeks
December 11, 2025 - The second case matches if the list has exactly three elements, binding them to x, y and z. If the list does not match either pattern, the wildcard _ is used to print "Unknown data format". A mapping is another common data type in Python and match-case can be used to match against dictionaries, checking for specific keys and values.
🌐
Mimo
mimo.org › glossary › python › match-statement
Python Match Statement: A Versatile Switch-Case in Python
http_status = 404 match http_status: case 200: print("OK") case 404: print("Not Found") case 500: print("Server Error") case _: # The wildcard `_` acts as the default case print("Unknown status") # Outputs: Not Found · The match statement is a powerful tool for structural pattern matching, which is more advanced than a simple if/elif chain. The Python syntax starts with the match keyword, followed by an expression.
🌐
LabEx
labex.io › tutorials › python-how-to-create-default-case-in-match-418553
How to create default case in match | LabEx
This tutorial explores techniques for creating default cases, providing developers with essential skills to handle complex matching scenarios and write more expressive, concise code. Python 3.10 introduced the match statement, a powerful pattern matching feature that provides a more expressive and concise way to handle complex conditional logic. Unlike traditional if-elif-else structures, match allows for more sophisticated pattern matching. def example_match(value): match value: case pattern1: ## Action for pattern1 return result1 case pattern2: ## Action for pattern2 return result2 case _: ## Default case (catch-all) return default_result
🌐
Stackademic
blog.stackademic.com › python-match-case-statement-63d01477e1c0
Python Match-Case Statement. Starting from Python 3.10, the Python… | by Caner Uysal | Stackademic
June 12, 2024 - You can use _ to specify a default case when the patterns don't match exactly. The match statement allows you to handle more complex scenarios by specifying additional conditions, dealing with nested patterns, and performing different actions based on the matched patterns. This way, you can process your data based on different cases and create cleaner and more readable code. ... Here are some differences between pattern matching in Python ...
🌐
DataCamp
datacamp.com › tutorial › python-switch-case
Python Switch Case Statement: A Beginner's Guide | DataCamp
December 6, 2024 - In web frameworks such as Django or Flask, you can use match-case to route HTTP requests or handle specific error codes. Learn more about Python for Developers with our online course. ... # Example: Handling HTTP methods in a Flask-like application method = "POST" match method: case "GET": print("Fetching resource...") case "POST": print("Creating resource...") case "PUT": print("Updating resource...") case "DELETE": print("Deleting resource...") case _: print("Unsupported HTTP method.") # Creating resource...
🌐
LabEx
labex.io › tutorials › python-how-to-define-default-case-in-matching-418555
How to define default case in matching | LabEx
def configure_system(config): match config: case {'mode': 'production', 'debug': False}: return "High-performance mode" case {'mode': 'development', 'debug': True}: return "Full debugging enabled" case {'mode': mode} if mode in ['staging', 'test']: return f"{mode.capitalize()} environment" case _: return "Invalid configuration" ... In the LabEx Python learning environment, developers can experiment with these advanced pattern matching techniques to write more expressive and concise code. By mastering default case patterns in Python's matching mechanism, developers can create more resilient and adaptable code.
Find elsewhere
🌐
Plain English Westminster
benhoyt.com › writings › python-pattern-matching
Structural pattern matching in Python 3.10
It has 217,000 lines of Python code, including tests. There are 1594 uses of elif, which again is 0.7%. Below are a couple of cases I saw which might benefit from pattern matching. Example from module_utils/basic.py, in _return_formatted().
🌐
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 - Let’s see the example below. def http_status(status): match status: case 400: return "Bad request" case 401: return "Unauthorized" case 403: return "Forbidden" case 404: return "Not found" case _: return "Other error" In the code above, we ...
🌐
Reddit
reddit.com › r/python › naming of the match default case
r/Python on Reddit: Naming of the match default case
June 14, 2021 -

Can anybody tell me why the default case is case _: instead of just default:? Is it to avoid confusion with the switch statement? But there is no switch statement in Python.

Python always prides itself on being readable and then this? Couldn't you just call it any other descriptive word instead of using the underscore? I know what it usually stands for, but this is a missed opportunity in my opinion.

🌐
Programiz
programiz.com › python-programming › match-case
Python match…case Statement
Let's solve the above error using the default case. operator = input("Enter an operator: ") x = 5 y = 10 match operator: case '+': result = x + y case '-': result = x - y case '*': result = x * y case '/': result = x / y case _: result = "Unsupported operator" print(result) ...
🌐
Tutorialspoint
tutorialspoint.com › home › python › python match case statement
Python Match Case Statement
February 21, 2009 - However, it allows you to include ... In the following example, the function argument is a list of amount and duration, and the intereset is to be calculated for amount less than or more than 10000....
🌐
Python
peps.python.org › pep-0622
PEP 622 – Structural Pattern Matching | peps.python.org
There is no default or else case - instead the special wildcard _ can be used (see the section on capture_pattern) as a final ‘catch-all’ pattern. Name bindings made during a successful pattern match outlive the executed suite and can be ...
🌐
Analytics Vidhya
analyticsvidhya.com › home › what is match case statement in python?
What is Match Case Statement in Python? - Analytics Vidhya
May 29, 2024 - The syntax of the match case statement in Python is as follows: match expression: case pattern1: # code block for pattern1 case pattern2: # code block for pattern2 ... case patternN: # code block for patternN case _: # default code block
🌐
LearnPython.com
learnpython.com › blog › python-match-case-statement
How to Use a match case Statement in Python 3.10 | LearnPython.com
Additionally, the second case has an if statement that matches only when the optional flag --ask is in the input. Below this, you could implement code to accept user input, then delete the files if the command is confirmed. Notice we had to select all the files to delete by using a list comprehension, which is a compact way of writing a for loop. Take a look at this article for more information on for loops in Python. The third case in the above example is matched when the optional flag is not in the input command.
🌐
Udacity
udacity.com › blog › 2021 › 10 › python-match-case-statement-example-alternatives.html
Python Match-Case Statement: Example & Alternatives | Udacity
September 27, 2022 - Next, we’ll show you how to use the match-case statement with Python 3.10, and what you can do if you’re using an older version of Python. As a programmer, you’ll often need to evaluate expressions against multiple values. Modern programming languages offer switch statements to do this. Here’s how a switch statement looks in pseudocode syntax: switch expression # the expression has a certain value case 1: # we test the value do something # the program performs an action depending on the value case 2: do something case 3: do something default: # if none of the case statements is satisfied do something # the program performs a default action
🌐
YouTube
youtube.com › shorts › __-FvBpyY18
How To Do Default Case In Python Match Case - YouTube
October 14, 2025 - This video shows how to do default case in Python match case. Basically, just case _: then whatever code you want ran in the default case.#python #shorts
🌐
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
🌐
Python Examples
pythonexamples.org › python-match-statement
Match Statement - Python Examples
But, if we would like to run some ... program. x = input('Enter a fruit name : ') match x: case "apple": print("Its red in color.") case "banana": print("Its yellow in color.") case "orange": print("Its orange in color.") case _: print("This fruit is not present in our shop.") ... Enter ...