Though we covered using operators like >=
and <=
for numbers like integers and floats, you can use these operators to compare strings as well. In the case of strings, it compares them alphabetically, with a few notable caveats. This is, however, used very infrequently, so consider it something of a bonus rather than required knowledge.
This tutorial builds off of concepts introduced in Numerical Comparisons and Combinations in Ren’Py, so you should make sure you’re familiar with numerical comparisons before reading this tutorial.
String Comparisons
Alphabetical Comparison
If you compare two strings with an operator like >
or <
, it compares them alphabetically. For example, you can do the following:
if 'apple' < 'banana':
# This is the one which will display
"apple comes before banana in the dictionary."
else:
# This won't be shown because the above condition is True
"banana comes before apple in the dictionary."
So, ‘apple’ comes before ‘banana’ because ‘a’ comes before ‘b’
Shorter words come earlier than longer ones:
if 'key' < 'keyhole':
# This is the one which will display
"key comes before keyhole in the dictionary."
else:
# This won't be shown because the above condition is True
"keyhole comes before key in the dictionary."
So, ‘key’ comes before ‘keyhole’ because ‘key’ is shorter.
…Except when it’s not alphabetical
The caveat I mentioned is that <
>
etc-style comparisons for strings do NOT take into account capitalization the way you might expect. In ASCII, A
comes before a
in ASCII codes (ASCII code 65 vs 97 – you can actually test this out on a computer! Hold down ALT and type 65 to get A
, and ALT+97 to get a
).
The reason I’m explaining this is because if you recall, "Apple" != "apple"
, since the comparison is case-sensitive. Well, the same is true here – if you try to compare “Apple” vs “apple” vs “APPLE”, the results may not be what you expect:
"Apple" == "apple"
# False; comparison is case-sensitive so these aren't the same
"apple" < "apple"
# False; these strings are identical, so it isn't accurate
# to say that 'apple' comes strictly before 'apple'.
"apple" <= "apple"
# True; "apple" == "apple" is True and <= means less than
# OR equal to, which is the important part, since these
# are equal.
"Apple" <= "apple"
# True; A comes before a in the ASCII 'dictionary' ordering
"Apple" < "apple"
# Also True; "Apple" and "apple" aren't equal, and "Apple"
# comes before "apple" due to ASCII ordering
"Apple" < "APPLE"
# False; the first letter is the same (A) so we compare p < P
# p comes *after* P in ASCII ordering (capital letters are
# all earlier than lowercase), so p < P is False.
# Notably, P=ALT+80, p=ALT+112 (and 112 < 80 is False).
"APPLE" < "apple"
# True; a comes after A in ASCII ordering.
Quiz
Now that you know a bit more about string comparisons, you can test your knowledge with these short quiz questions.
Summary
In summary, it’s important to remember that just like when comparing if two strings are equal, capitalization is very important when using <=
, <
, >
, and >=
to compare strings, since they are compared using their corresponding ASCII codes to determine the result of a “less than”/”greater than” comparison. If capitalization is the same and the first letters are the same, shorter words will come before longer ones.
Next Steps
The final part in this series about conditionals, comparisons, and variables in Ren’Py is Combining Comparisons in Ren’Py. See you there!