Home > Net >  why "a" is bigger than "A" in python? [duplicate]
why "a" is bigger than "A" in python? [duplicate]

Time:10-04

print(4 > 5)

output is False this is very easy to understand using basic math

print("a" > "A")

output is True

how does python compare a and A ?

CodePudding user response:

Python string comparison is performed using the characters in both strings. The characters in both strings are compared one by one. When different characters are found then their Unicode value is compared. The character with lower Unicode value is considered to be smaller.

The Unicode value of 'A' is 65, whereas the for 'a' it is 97.

The ord() function returns the Unicode value of a character.

ord('A')  # returns 65
ord('a')  # returns 97
ord('AA')  # ERROR: ord() expects a string of length 1.

CodePudding user response:

"a" ascii is 97 --> ord("a")

"A" ascii is 65 --> ord("A")

Hence:

print("a" > "A") --> True
  • Related