+3 votes
in Programming Languages by (77.7k 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

+1 vote
by (355k points)
selected by
 
Best answer

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)


...