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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
>>> "tim" == "tie"
False
>>> "free" != "freedom"
True
>>> "arrow" > "aron"
True
>>> "right" >= "left"
True
>>> "teeth" < "tee"
False
>>> "yellow" <= "fellow"
False
>>> "abc" > ""
True
>>>
|
No comments:
Post a Comment