ADVERTISEMENT
ADVERTISEMENT

How to check if a string contains special characters or not in Python?

Here, we will get a string as input from the user and check if the string contains a special character or not using a Python program.
Submitted by Shivang Yadav, on April 18, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Strings in Python are immutable means they cannot be changed once defined.

Special characters are characters other than alphabets. The set contains "[@_!#$%^&*()<>?/\|}{~:] ".

How to check if a string contains any special character?

To check for the presence of any special character in a string, we will compare all special characters for characters in the string. An effective way to do this is using regular expressions which provides methods for comparison.

We will create a regular expression consisting of all characters special characters for the string. Then will search for the characters of regex in the string. For this, we will use the search() method in the "re" library of Python.

The search() method is used to check for the presence of a special character in the string. It returns boolean values based on the presence.

Syntax:

regularExpName.search(string)

Program to check if the string contains any special character

# Python program to check if a string 
# contains any special character 

import re

# Getting string input from the user 
myStr =  input('Enter the string : ')

# Checking if a string contains any special character  
regularExp = re.compile('[@_!#$%^&*()<>?/\|}{~:]')

# Printing values 
print("Entered String is ", myStr)
if(regularExp.search(myStr) == None):
    print("The string does not contain special character(s)")
else:
    print("The string contains special character(s)")

Output:

RUN 1:
Enter the string : Hello, world!
Entered String is  Hello, world!
The string contains special character(s)

RUN 2:
Enter the string : [email protected]#%^
Entered String is  [email protected]#%^
The string contains special character(s)

RUN 3:
Enter the string : Hello world
Entered String is  Hello world
The string does not contain special character(s)

Python String Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.