Why “cat” is not equal to “dog” in Python [duplicate]

Because python compares sequences (i.e. strings you have) sequentially by element values. “c” comes before “d” in Unicode, hence the result.

To compare length, use

len(a) == len(b)

The motivation behind this – having sequential comparison of elements, you can sort sequences in sane way. I.e. sorting list of names alphabetically is just sorted(names_list). And this works exactly because strings will be compared alphabetically and sequentially.

Browse More Popular Posts

Leave a Comment