Program to check whether the string contains the defined character using Python

Introduction

In this article our task is to search whether the given input string contains specific characters.

Program to check whether the string contains the defined character using Python

Program

import re
def verify(ip_str, re_exp):
    if re.search(re_exp, ip_str):
        print("{0} is present in the expression {1}".format(ip_str, re_exp))
    else:
        print("{0} is not present in the expression {1}".format(ip_str, re_exp))
       
re_exp = re.compile('[1234567]+!$' )
ip_str = input("Enter teh string to check: ")
verify(ip_str, re_exp)

Output

Program to check whether the string contains the defined character Output

Explanation

In the above code we have used compile() function and search() function of the regression module. Using compile() we complied the expression into pattern object. And to search the input string in the pattern we have used search().

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.