Python : How to compare strings and ignore white space and special characters
29,145
Solution 1
This removes punctuation and whitespace before doing the comparison:
In [32]: import string
In [33]: def compare(s1, s2):
...: remove = string.punctuation + string.whitespace
...: return s1.translate(None, remove) == s2.translate(None, remove)
In [34]: compare('Hai, this is a test', 'Hai ! this is a test')
Out[34]: True
Solution 2
>>> def cmp(a, b):
... return [c for c in a if c.isalpha()] == [c for c in b if c.isalpha()]
...
>>> cmp('Hai, this is a test', 'Hai ! this is a test')
True
>>> cmp('Hai, this is a test', 'Hai this is a test')
True
>>> cmp('Hai, this is a test', 'other string')
False
This creates two temporary lists, but doesn't modify the original strings in any way.
Solution 3
Generally, you'd replace the characters you wish to ignore, and then compare them:
import re
def equal(a, b):
# Ignore non-space and non-word characters
regex = re.compile(r'[^\s\w]')
return regex.sub('', a) == regex.sub('', b)
>>> equal('Hai, this is a test', 'Hai this is a test')
True
>>> equal('Hai, this is a test', 'Hai this@#)($! i@#($()@#s a test!!!')
True
Solution 4
To compare an arbitrary number of strings for alphabetic equivalence,
def samealphabetic(*args):
return len(set(filter(lambda s: s.isalpha(), arg) for arg in args)) <= 1
print samealphabetic('Hai, this is a test',
'Hai ! this is a test',
'Hai this is a test')
Which prints True
. Should change <=
depending on what you want to return for no arguments.
Related videos on Youtube

Author by
Rohith
Updated on October 14, 2021Comments
-
Rohith over 1 year
I want to compare two strings such that the comparison should ignore differences in the special characters. That is,
Hai, this is a test
Should match with
Hai ! this is a test "or" Hai this is a test
Is there any way to do this without modifying the original strings?
-
Dan Lecocq about 10 yearsWhat are 'special characters' in this context? Importantly, are spaces counted? Because then ignoring the '!' leaves two spaces as opposed to the one in the first string.
-
-
DSM about 10 yearsInstead of
c in string.letters
, you can usec.isalpha()
. -
Adam Dobrawy over 6 yearsIt is not Python3 compatible.
-
Martijn Pieters about 6 years@AdamDobrawy: Use
str.maketrans(dict.fromkeys(remove))
as the map (first argument).