String comparison question

Fredrik Lundh fredrik at pythonware.com
Mon Mar 20 08:25:15 EST 2006


luc.saffre at gmail.com wrote:

>> > I would like to make a string comparison that would return true without
>> > regards of the number of spaces and new lines chars between the words
>> >
>> > like 'A   B\nC' = 'A\nB    C'
>
> Here is how I do such comparisons:
>
>  if a.strip().split() == b.strip().split()

clever solution (I was about to post a split/join solution, but the join is of course
meaningless), but the strip() isn't necessary: the default version of split already
removes leading and trailing whitespace:

>>> " hello world ".split()
['hello', 'world']

>>> " hello world ".split(None)
['hello', 'world']
>>> " hello world ".split(" ")
['', 'hello', 'world', '']

</F> 






More information about the Python-list mailing list