print 'Python' > 'python' # equals False
print 'python' > 'Python' # equals True
Can someone please explain how this is interpreted since p
is smaller case then then capital P
? But yet p
is always greater then P
.
Tested on Python 2.7
print 'Python' > 'python' # equals False
print 'python' > 'Python' # equals True
Can someone please explain how this is interpreted since p
is smaller case then then capital P
? But yet p
is always greater then P
.
Tested on Python 2.7
String comparison in Python is case-sensitive and conventionally uppercase characters go before lowercase characters.
Python compares strings lexicographically, using the constituent characters based on their ASCII or Unicode code points. The same principle applies for Python3.
In ASCII, and therefore in Unicode, lowercase letters are greater than all uppercase letters. Therefore, 'p' > 'P'
, and indeed, 'a' > 'Z'
. In your case, "python"
begins with the letter 'p'
, whereas "Python"
begins with the uppercase letter 'P'
. They begin with different code points; the lowercase variant is greater.
The convention that lowercase letters are greater than uppercase letters in ASCII is historical.
It may have something to do with the unicode values of the letters.
>>> ord('p')
112
>>> ord('P')
80
112 > 80
, therefore 'p' > 'P'