Why is 'a' greater than 'A' in python?
Question:
First I tried this:
print('a' > 'A')
The above statement returns true
Then I tried this:
print('A' > 'a')
The above statement returns false
What is the reason?
Answers:
Checkout out ASCII table here. The comparison compares ASCII codes of a
and A
.
In case of strings, Python compares the ASCII(American Standard Code of Information Interchange) values of the characters.
Because it has a higher unicode value. To check you can use:
>>> ord('a')
97
>>> ord('A')
65
According to the ASCII table, value of ‘A’ is 65 and ‘a’ is 97.
So if u compare a single character between a-z,A-Z Python takes it’s ASCII value.
You can check the ASCII table from here
First I tried this:
print('a' > 'A')
The above statement returns true
Then I tried this:
print('A' > 'a')
The above statement returns false
What is the reason?
Checkout out ASCII table here. The comparison compares ASCII codes of a
and A
.
In case of strings, Python compares the ASCII(American Standard Code of Information Interchange) values of the characters.
Because it has a higher unicode value. To check you can use:
>>> ord('a')
97
>>> ord('A')
65
According to the ASCII table, value of ‘A’ is 65 and ‘a’ is 97.
So if u compare a single character between a-z,A-Z Python takes it’s ASCII value.
You can check the ASCII table from here