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


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 (28.9k 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.


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