Python - Comparing two string numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Python - Comparing two string numbers

Can someone explain me this? print("6" < "12") output// False Why false?

26th Apr 2021, 10:48 AM
Kakai
Kakai - avatar
4 Answers
+ 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!
26th Apr 2021, 11:08 AM
Abhiyantā
Abhiyantā - avatar
+ 2
Strings are compared alphabetically not numerically . As string "6" > "1" so "6"<" 12" outputs false.
26th Apr 2021, 10:52 AM
TOLUENE
TOLUENE - avatar
+ 1
Rishav Tiwari thank you, now i need to research what's ascii hahaha btw, thanks.
26th Apr 2021, 12:06 PM
Kakai
Kakai - avatar
0
Sayed🇧🇩🇧🇩 i still don't get it. Can you explain more please? Thank you.
26th Apr 2021, 10:55 AM
Kakai
Kakai - avatar