How to check if a string contains a Special Character or Not in Python
In this tutorial, you will learn how to check if a string contains a special character or not in Python programming language.
Special characters are those characters that have a built-in meaning in the programming language. These can be either a single character or a set of characters.
Here are some examples:
Python<Language String contains Special Characters Python Language String does not contain Special Characters
To check the presence of special characters we create a regular expression object (string_check) of all the special characters and pass it into the search function. The search function matches all the characters of the input string to the set of special characters specified in the Regular Expression object (string_check). If there is a match then it returns the matched character otherwise it will return None.
Python program to check if a string contains a special character or not
Below is the given Python program that will find if the string contains special character or not:
#Python program to check if a string contains #any special characters or not # import required package import re # Function checks if the input string(test) # contains any special character or not def check_splcharacter(test): # Make an RE character set and pass # this as an argument in compile function string_check= re.compile('[@_!#$%^&*()<>?/\|}{~:]') # Pass the string in search function # of RE object (string_check). if(string_check.search(test) == None): print("String does not contain Special Characters.") else: print("String contains Special Characters.") # Driver Code if __name__ == '__main__' : # Enter the string to be checked test = "Code%Speedy" # calling check_splcharacter function check_splcharacter(test)
OUTPUT:
String contains Special Characters
First, we import the required package from the Python library.
import re
We define a function check_splcharacter and pass a string argument(Test). Then create a Regular Expression (string_check) containing all the special characters using the re.compile function. Pass the argument string (ie test) in the search function.
The search function matches each character present inside the ‘test’ string with the special characters present in the regular expression. If there is a match it returns the character that matched otherwise it returns None.
If the result is None then the output will be ” String does not contain Special Characters” else the output will be “String contains Special Characters”
def check_splcharacter(test): # Make own character set and pass # this as argument in compile method string_check= re.compile('[@_!#$%^&*()<>?/\|}{~:]') # Pass the string in search # method of regex object. if(string_check.search(test) == None): print("String does not contain Special Characters.") else: print("String contains Special Characters.")
You may also read:
how to perform the same in dataframe as well awaiting for your response
Use apply operation in dataframe.
Something like:
def check_splcharacter(test):
# return True / False based on whether it contains a special character or not
df[‘contains_special_character’] = df[‘string’].apply(lambda x: check_splcharacter(x))
Then filter for
df[df[‘contains_special_character’] == True]
This is not working for me. It returns False no matter what I throw at it. I want a True return if the string only contains numbers and dashes.
I am using it like this:
def checkStringForUnallowables(test):
stringCheck = re.compile(‘abcdefghijklmnopqrstuvwxyz[@_!#$%^&*()?/\|}{~:]’)
if(stringCheck.search(test) == None):
print(“String does not contain unallowables – returning False”)
return False
else:
print(“String contains unallowables – returning True”)
return True
does it had a simple looping method. pls tell that also we dont want to go into modules