+ 3
Python - Comparing two string numbers
Can someone explain me this? print("6" < "12") output// False Why false?
4 Réponses
+ 3
Hi Kakai,
when you compare two string it checks ASCII values.
for eg,
>>> 'A' < 'a'
True
Because the ASCII of 'A' is 63 and 'a' is 97 ,means small is Greater, but if some strings are same like 'aaa' and 'aaA' ,then 'aaa' will be bigger because now it will check from those which not match, I means in those strings index 0 and 1 contains 'a' , py will compare non-diff in this case means 'a' and 'A'. If the strings are different then it will check from that side where order not match.
In your code "6" and "12" , so the ASCII of "6" is 54 and from "12" , it will take "1" because both index 0 are not equal and ASCII of "1" is 49,
And we all know that 54 is Greater that 49 so it is false
https://code.sololearn.com/cKwRQ6xBjVrl/?ref=app
Analyze this code!
All the best!
+ 2
Strings are compared alphabetically not numerically .
As string "6" > "1" so "6"<" 12" outputs false.
+ 1
Rishav Tiwari thank you, now i need to research what's ascii hahaha btw, thanks.
0
Sayed🇧🇩🇧🇩 i still don't get it. Can you explain more please? Thank you.