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

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts

Comments

Leave a Reply

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.