0

I usually test existence of a substring in a string via in:

In [3]: x = 'Hello World'

In [5]: 'rl' in x
Out[5]: True

How can I extend this to test for the existence of one (or more) of several substrings in one string?

I specifically would like to avoid using a chain of or:

In [6]: 'rl' in x or 'ld' in x
Out[6]: True

(the set of substrings will be variable)

WoJ
  • 27,165
  • 48
  • 180
  • 345

1 Answers1

0

Use any

any(i in x for i in ('rl', 'ld'))

Example:

>>> x = 'Hello World'
>>> any(i in x for i in ('rl', 'ld'))
True
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274