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 an inbuilt switch statement. In this article, we will see how we can achieve the switch statement functionality in Python using a dictionary-based approach and class-based approach, and lambda-based approach.

The Switch Statement in Other Programming Languages

Before understanding how Python implements switch statements we need to understand how to 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)()

Output

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)()

Output

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 getattr() method to get the method 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"])()

Output

Today is Monday

Conclusion

Python does not have an inbuilt function to implement switch statements like other programming languages. So in Python, we use different approaches like dictionary-based approach, class-based approach, and lambda-based approach to implementing switch statements. In this, we understood all three approaches using appropriate examples.

Updated on: 17-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements