Try This: Accept in Any Case

In the case of string.replace("It", "A String") in the previous example script, there was only one instance of it, so it worked as expected.  Unlike the approach from String Surgery, the string.replace() method replaces all instances of a substring by default.  So, if you’re not careful, and choose a substring like "a", you might see some surprising results!

The string.replace() method does have an optional argument to limit the number of replacements it makes in the string.

One use for the string.lower() method is to make menu systems accept whatever the user types, regardless of what combination of uppercase and lowercase characters have been used.  This can be helpful in menu systems, because we might just want the script to do what it’s told, regardless of whether we typed run, RUN, Run, RUn, or some other combination.  By calling string.lower() first, it makes it so the program only has to check for one word: run

Example script: other_methods_try_this

This next example is a modified version of comp_find_check_your_turn from Compare, Find, Check.  By adding a single string.lower() call, we can avoid checking for every conceivable combination of run, Run, RUN, RuN, etc.  

  • Open comp_find_check_your_turn and save it as other_methods_try_this.  
  • Flash the script into the micro:bit
# other_methods_try_this

from microbit import *

sleep(1000)

while True:
    s = input("Type run: ")
    s = s.lower()                # <- Add this line
    n = s.find("run")
    if(s == "run"):
        print("Good, you typed run.")
    elif(n != -1):
        print("It contains run.")
        print("r is the", n, "th character.")
    else:
        print("You didn't type run.")
  • Open the terminal.
  • Try typing run, Run, RUN, and some other combos, and verify that all are accepted as “run”.