python regex string contains substring code example

Example 1: python regex from list of string that contains substring

# Python3 program to Filter list of 
# strings based on another list
import re
  
def Filter(string, substr):
    return [str for str in string 
    if re.match(r'[^\d]+|^', str).group(0) in substr]
      
# Driver code
string = ['city1', 'class5', 'room2', 'city2']
substr = ['class', 'city']
print(Filter(string, substr))

Example 2: python check if string contains substring

string = "My favourite programming language is Python"
substring = "Python"

if substring in string:
    print("Python is my favorite language")
elif substring not in string:
    print("Python is not my favourite language")