11A: Lower Case

Lesson 11 has three parts A, B, C which can be completed in any order.

This lesson contains an exercise where you need to write two functions: one will use the other to accomplish its goal. The goal is to eventually write a function lowerString that can convert all of the letters in a string to lower case. (A, B, C are upper case letters and a, b, c are lower case.) For example, the result of

lowerString("This string has 9 CAPITAL letters (& Punctuation)!")
should be

"this string has 9 capital letters (& punctuation)!"

Step 1: Characters

The first step is to write a function lowerChar(char) that can return the result of converting a single character char to lower case. It should do the following:

  • if the input character char is a capital letter (between 'A' and 'Z'), it should return the lower-case version of the letter (between 'a' and 'z')
  • in all other cases, it should return the same char which was input.

(In order to do the first step, you will have to use an if statement, an and operator, and apply some knowledge from the lesson about strings.)

Coding Exercise: Lower-case Characters
Define a function lowerChar(char) which meets the above description.
Enter testing statements like print(myfunction("test argument")) below.

Step 2: Strings

Now, you will write a second function lowerString(string) which will return the result of converting the entire string to lower case, by calling lowerChar on each character. We suggest you do this as follows:

  • first, copy the definition of lowerChar(char) from your solution to the first part
  • then define a second function, lowerString(string)
    • on the first line inside lowerString, initialize a variable result = "" equal to the empty string
    • use a for loop with i and set result = result + lowerChar(string[i])
    • finally, return result

Coding Exercise: Lower-case Strings
Define a function lowerString(string) which returns the result of converting string to lower case.
Enter testing statements like print(myfunction("test argument")) below.

Later on, you will learn about the string.lower() method, which is a built-in way to perform this task.