Is there a “in” operator in python 3.10 Match Case like with if else statements
if "n" in message:
the in operator doesn’t work in match case
match message: case "n" in message:
This doesn’t work. How to have something like the “in” operator in Match-Case.
Advertisement
Answer
In match-case
we can use the split()
method on the message string.
Example:
def match_case_with_if(command): match command.split(): case [*command_list] if 'install' in command_list: print(f"Hey you are installing {command_list[-1]}....") case [*command_list] if 'uninstall' in command_list: print(f"Are you sure you want to uninstall {command_list[-1]} ?") match_case_with_if('pip install numpy') # Output: Hey you are installing numpy.... match_case_with_if('pip uninstall numpy') # Output: Are you sure you want to uninstall numpy ?
Here, the string can be split into a list and if condition can be applied on it’s contents. Hope this helps!!