Reputation: 183
I would like to find a more elegant way of coding the next:
str_example = "u are my // path"
if '//' in str_example or '/' in str_example:
do something
Upvotes: 2
Views: 78
Reputation: 6776
If there are only two (possibly three) short things to check, I’d probably leave the code as it is. If something can be expressed with a short piece of very simple code, why not do that?
Four (possibly three) or more, I’d go with the any(...)
variants that several others have already suggested.
Upvotes: 1
Reputation: 11061
use all()
and any()
functions
str_example = "u are my // path"
if any(s in str_example for s in ['//', '/']):
pass
https://docs.python.org/3/library/functions.html#all
Upvotes: 5
Reputation: 15247
You can store the substrings in an array and use something like :
needles = ['//', '/']
if any(needle in str_example for needle in needles):
do something
Upvotes: 3