+3 votes
in Programming Languages by (17.2k points)
How to check if two given strings are equal? One of the two strings can have blanks at the end.

E.g.

a= 'Hello'

b='Hello '

If I compare these two strings using ==, it returns false. I want True as the answer.

1 Answer

0 votes
by (30.3k points)

You can use strip() function on both strings and then "==" will return True. Also, you can use Numpy's char module. It has a function equal() that compares strings by first stripping whitespace characters from the end of the strings.

E.g.

>>> from numpy import char
>>> x1='Hello'
>>> x2='Hello '
>>> x1==x2
False
>>> x1.strip()==x2.strip()
True
>>> char.equal(x1,x2)
array(True)

...