Change the Case (Solution)
00:00 The task is to create four strings, convert them all to lowercase, and then convert them all to uppercase, each on a separate line.
00:09
I’m going to start off by defining the strings and assigning them to variables. string1
was "Animals"
in title case, string2
was "Badger"
, string3
"Honey Bee"
, and string4
00:33
“Honey Badger”`, the most dangerous of them all. All right, and now I’m going to write calls to print(string1.lower())
, close it, and I get animals
as an output. string2.lower()
, so this is a bit of a typing exercise as you can see, because you’re really just redoing the same thing for each of the strings.
01:01 And later on in the learning path, you will also learn how to do this a bit more automated so that you wouldn’t have to type so much. But for now, it does not hurt to get a bit of typing exercise and just practice using these string methods and how you apply them to a string, which is by first writing the string, then a dot, and then the string method.
01:22
And you use these parentheses here—in this case, they’re empty—to call this method. So it’s similar to the print()
function as you can see here, where you also need the parentheses.
01:32
We converted all of them to lowercase. As you can see, the output is in all lowercase. And now we want to do the same, but convert them to uppercase. So we’ll say string1.upper()
,
01:44
and you can see that ANIMALS
gets returned in all uppercase. Let’s continue the typing exercise, string2.upper()
,
01:55
string3.upper()
, and finally string4.upper()
. I’m a little scared of uppercasing HONEY BADGER
. Who knows what’s going to happen?
Become a Member to join the conversation.
Stephen Smith on Dec. 13, 2023
Because I’m lazy and to save typing I put the strings into a list, and created functions to loop over the list ;)
list_A = [“Animals”, “Badger”, “Honey Bee”, “Honey Badger”]
def down(): for element in list_A: print(element.lower(), sep=”\n”)
down()
def up(): for element in list_A: print(element.upper(), sep=”\n”)
up()