Python compare strings ignore special characters -
i want compare 2 strings such comparison should ignore differences in special characters. is,
hai, test
should match
hai ! test "or" hai test
is there way without modifying original strings?
this removes punctuation , whitespace before doing 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, test', 'hai ! test') out[34]: true
Comments
Post a Comment