8.6 Comparison Operators for Strings

Strings may be compared with the comparison operators. Recall that strings are compared based on their underlying integer numeric values. So uppercase letters compare as less than lowercase letters because uppercase letters have lower integer values. For example, 'A' is 65 and 'a' is 97. You’ve seen that you can check character codes with ord:

In [1]: print(f'A: {ord("A")}; a: {ord("a")}')
A: 65; a: 97

Let’s compare the strings 'Orange' and 'orange' using the comparison operators:

In [2]: 'Orange' == 'orange'
Out[2]: False
    
In [3]: 'Orange' != 'orange'
Out[3]: True
    
In [4]: 'Orange' < 'orange'
Out[4]: True
    
In [5]: 'Orange' <= 'orange'
Out[5]: True
    
In [6]: 'Orange' > 'orange'
Out[6]: False
    
In [7]: 'Orange' >= 'orange'
Out[7]: False
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.185.180