Thursday, 29 November 2018

11-C String comparison in python

You can use ( > , < , <= , <= , == , !=  ) to compare two strings. Python compares string lexicographically i.e using ASCII value of the characters.
Suppose you have str1  as "Mary"  and str2  as "Mac" . The first two characters from str1  and str2 ( M  and M ) are compared. As they are equal, the second two characters are compared. Because they are also equal, the third two characters ( r  and c) are compared. And because 'r'  has greater ASCII value than 'c' , str1  is greater than str2 .
Here are some more examples:

No comments:

Post a Comment