How is '120' greater than '1000' in string format? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How is '120' greater than '1000' in string format?

https://code.sololearn.com/czRR4IY19PsJ/?ref=app

18th Apr 2020, 7:56 AM
SAHIL
SAHIL - avatar
5 Answers
+ 1
Because strings are not numbers. Take the individual numbers placement. 1 is the first number for each element they are both equal. But 120 has a "2" as the second digit and 1000 has "0". 2 is higher than 0 so '120' is bigger. Look at the other example print('9' > '800') O/p:- True Since '9' is greater than '8' Therefore it's output is True Because both on their first place They have different numbers Since '9' is greater than '8' Therefore it's output is True
1st May 2020, 2:42 PM
SAHIL
SAHIL - avatar
+ 7
String comparation is according to ASCII of each character. You can get ASCII of a character using ord(). Because 2 > 0 in ASCII, thus '120' is greater than '1000'.
18th Apr 2020, 9:01 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 4
FYI, Python 3 actually uses unicode (UTF-8) encoding by default, which is basically an 8 bit expanded version of the ascii table. String comparisons are done lexicographically in the manner that CarrieForle stated.
18th Apr 2020, 10:16 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
According to ASCII code 120 is greater than 1000,it is due to string comparison occur in ASCII code.
19th Apr 2020, 3:07 AM
Sâñtôsh
Sâñtôsh - avatar
+ 1
in string it is same because it's string, not number or double or integer so in string that is same, sory i don't know ASCII
19th Apr 2020, 6:58 AM
Yakobus Patrick
Yakobus Patrick - avatar