I've got this method to recognize if there are any special characters in a given string, and it works just fine when the string is one word only such as 'h3llo'
, it will return True
(there are special characters inside my string), but when i add spaces to it (which is what has me struggling because of what i need the code for) it returns True even when there are no special characters in it at all, such as hello how are you
, it will return True
, because of the spaces. I need to fix my method to ignore the spaces, but I've ran out of ideas. I'd appreciate some help!! Thanks in advance :)
This is my function:
def has_spchar(inputString):
return any(not char.isalnum() for char in inputString)
CodePudding user response:
def has_spchar(inputString):
return any(not (char.isalnum() or char.isspace()) for char in inputString)
It might be more efficient, clearer, and easier to modify if you create a set
of the characters that are not special and test against that.