This example program introduces four more useful string methods: replace(), upper(), lower(), and split().
# other_methods_intro from microbit import * sleep(1000) string = "This is a string. It is a sequence of characters!" print("Original string:", string) print() lower_case = string.lower() upper_case = string.upper() print("lower_case =", lower_case) print("upper_case =", upper_case) print() new_string = string.replace("It", "A string") print("new_string =", new_string) print() new_list = string.split() print("new list =", new_list) print()
The first routine declares a string, names it string, and then prints it for reference.
string = "This is a string. It is a sequence of characters!" print("Original string:", string) print()
The string.lower() and string.upper() methods return lower and upper case versions of the original string, and print them:
lower_case = string.lower() upper_case = string.upper() print("lower_case =", lower_case) print("upper_case =", upper_case) print()
The string.replace() method is another way to replace parts of a string. It’s not as straightforward as the approach from the previous String Surgery section, because it would replace all instances that matche the substring, if there were more than one "It".
new_string = string.replace("It", "A string") print("new_string =", new_string) print()
The string.split() method splits a string with separators into substrings. By default, the separator is a space, but you could pass it a comma, for example. Many data strings are comma delimited, and need to be split before each individual data item can be examined.
new_list = string.split() print("new list =", new_list) print()
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
This next example is a modified version of comp_find_check_your_turn from Compare, Find, Check [1]. By adding a single string.lower() call, we can avoid checking for every conceivable combination of run, Run, RUN, RuN, etc.
# 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.")
As mentioned on the previous page, the string.replace() method is designed to find and replace all parts of a string that match the substring. If there’s only going to be one match, great. What if you instead want to replace the word “a” with “the” though?. If the original string contains the word “characters”, it might get converted to “chtherthecters”. Yikes!
To solve this, the substring could be set to " a " with spaces before and after. Likewise, the replacement string is should be " the ", also with spaces before and after.
And let’s say you only want to replace the first instance of “a”. Another approach would be to use string.replace(" a ", " the ", 1).
# other_methods_your_turn from microbit import * sleep(1000) string = "This is a string. It is a sequence of characters!" print("Original string:", string) print() new_string = string.replace("a", "the") print("Unexpected results with 'a' -> 'the':") print("new_string =", new_string) print() new_string = string.replace(" a ", " the ") print("Better results with 'a' -> 'the':") print("new_string =", new_string) print() new_string = string.replace(" a ", " the ", 1) print("Selective results with 'a' -> 'the':") print("new_string =", new_string) print()
Links
[1] https://learn.parallax.com/tutorials/language/python/strings-characters-primer/compare-find-check