Search code examples
pythonstringeval

What's the difference between?


string1 = "15 > 4 "
string2 = "'15' > '4'"
print(eval(string1))
print(eval(string2))
True
False

Just like the title says. Why do you have different values for the same string?


Solution

  • No these are different, because the first one compares two integers, but the second one compares to strings.

    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.

    and here ord('1') is 49 and ord('4') is 52, so 49 < 52 I mean '1' < '4' so it is False.