Program to find special character in the given string using Python

by | Mar 5, 2021 | Python Programs

Home » Python » Python Programs » Program to find special character in the given string using Python

Introduction

The task is to find the special characters in the string and if it exists reject the string otherwise accept the string.

Program to find special character in the given string using Python

Program

import re


input_str = input("Enter teh string : ")
#Passing char set as argument in compile method
reg_obj = re.compile('[@_!#$%^&*()<>?/\|}{~:]')


#Search reg_obj in input string
if(reg_obj.search(input_str) == None):
    print(" '{0}' is accepted! It ahs no special character!".format(input_str))
else:
    print(" '{0}' is not accepted! It has special characters!".format(input_str))

 

Output

Program to find special character in the given string Output

Explanation

In the above python code we have passed the set of special characters in compile function of regex. The search function in if condition will check whether the special character is found in the input string. If the character is found, the string is not accepted else it is accepted.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

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

Author