Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to implement switch statement on String
In Python, we can implement switch statements on a string using the dictionary-based approach, class-based approach, and lambda-based approach. Unlike other programming languages like Java, C++, etc., Python does not have a built-in switch statement. In this article, we will see how we can achieve the switch statement functionality in Python using different approaches.
The Switch Statement in Other Programming Languages
Before understanding how Python implements switch statements, we need to understand how switch statements work and how they are implemented in other programming languages.
A switch statement is a conditional statement that evaluates an expression and performs different actions based on its values. It contains several code block cases which are executed based on the evaluated expression.
Syntax
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}
The switch() statement takes an expression and based on the evaluation of that expression/value it executes one of the cases out of many cases defined in the switch case.
Implementing Switch Statement in Python
Method 1 Using a Dictionary-based Approach
In this method, we create a dictionary that maps the string value to corresponding functions. Once the mapping is done we can call the function associated with the input string.
Example
In the below example, we created a separate function for printing the days Monday and Tuesday and a default function if the day is neither Monday nor Tuesday. We then map the string Monday and Tuesday with their corresponding function using a Python dictionary ?
def monday():
print("Today is Monday")
def tuesday():
print("Today is Tuesday")
def default():
print("Today is not Monday or Tuesday")
switch = {
"Monday": monday,
"Tuesday": tuesday,
}
day = "Monday"
switch.get(day, default)()
The output of the above code is ?
Today is Monday
Method 2 Using the Class-Based Approach
In this method, we create a class that contains a method for each case. Once the methods are implemented inside the class we can call the corresponding method associated with the input string.
Example
In the below example, we created a class switch that contains methods for printing different days like Monday, and Tuesday and a default function if the day is neither Monday nor Tuesday. We can then call the getattr() method to get the method associated with the respective string being passed ?
class Switch:
def monday(self):
print("Today is Monday")
def tuesday(self):
print("Today is Tuesday")
def default(self):
print("Today is not Monday or Tuesday")
switch = Switch()
day = "Monday"
getattr(switch, day.lower(), switch.default)()
The output of the above code is ?
Today is Monday
Method 3 Using Lambda-Based Approach
In this method, we create a dictionary that maps the string values to corresponding lambda functions. We can then call the lambda function associated with the input string.
Example
In the below example, we create a dictionary switch and map the string Monday, and Tuesday and default to their respective lambda function. We then call the get() method to get the lambda function associated with the input string day ?
switch = {
"Monday": lambda: print("Today is Monday"),
"Tuesday": lambda: print("Today is Tuesday"),
"default": lambda: print("Today is not Monday or Tuesday")
}
day = "Monday"
switch.get(day, switch["default"])()
The output of the above code is ?
Today is Monday
Comparison
| Method | Pros | Cons |
|---|---|---|
| Dictionary-based | Clean, reusable functions | Requires separate function definitions |
| Class-based | Organized, extensible | More complex for simple cases |
| Lambda-based | Compact, inline logic | Limited to simple expressions |
Conclusion
Python does not have a built-in switch statement, but we can implement similar functionality using dictionaries, classes, or lambda functions. The dictionary-based approach is most commonly used for its simplicity and readability.
