How to Write a Conditional Expression that Does Not Match Any of Multiple Strings in Python

Asked 2 years ago, Updated 2 years ago, 35 views

Is there any other way for Python 3 to write a conditional expression that does not match any of the multiple strings (the state that the application keeps) short, other than the if statement below?

status='status_invalid'
if(status!='status_1') and(status!='status_2') and(status!='status_3'):
    print('invalid status')

python python3

2022-09-30 19:24

1 Answers

If the string comparison is a simple full-text match, I think you can use not in.

status='status_invalid'
if status not in ['status_1', 'status_2', 'status_3']:
  print('invalid status')


2022-09-30 19:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.