The aim of this challenge is to rewrite the upper() function that is used in Python to convert the case of a string to uppercase.
This is how the upper() function works in Python:
string = "Hello world!" upString = string.upper() print(upString)
For this challenge we will write a function called uppercase() that takes a string as a parameter and returns the uppercase version of this string. However, to complete this challenge we are not allowed to use the pre-defined upper() or lower() Python functions!
Our Python function will need to:
- Access each character of the given string
- Find out if the character is a lowercase letter of the alphabet
- If so, convert this character to its uppercase equivalent. (We will do so using the ASCII code!)
- Append the character to a new string
- Return the new string once all character have been checked.
ASCII Code
Each character that you can use on your keyboard has a unique ASCII value, a number between 0 and 255. This includes the 26 letters of the lowercase alphabet, the 26 letters of the uppercase alphabet, the number digits 0 to 9 and all the punctuation signs such as !,?.#, etc. Even the space bar has a unique ASCII value: 32.
Here are the ASCII values of the lowercase alphabet:
Character | a | b | c | d | e | … | x | y | z |
ASCII | 97 | 98 | 99 | 100 | 101 | … | 120 | 121 | 122 |
And below are the ASCII values of the uppercase alphabet:
Character | A | B | C | D | E | … | X | Y | Z |
ASCII | 65 | 66 | 67 | 68 | 69 | … | 88 | 89 | 90 |
So, if we have the ASCII value of a lowercase character, we can retrieve the ASCII value of its matching uppercase character by taking away 32 from the ASCII value of the lowercase character.
For instance:
And…
Python Code and ASCII Code
In Python, you can use the ord() and chr() functions to convert a character into its ASCII value and vice-versa.
For instance, the ord() function is used get the ASCII value of any given character:
character = "A" ascii = ord(character) print(ascii) #Output 65
The chr() function returns the character for any given ASCII value (number between 0 and 255).
ascii = 65 character = chr(ascii) print(character) #Output A
Also, to find if a character is a character from the lowercase alphabet, all we need to do is check that its ASCII value is between 97 and 122.
character = input("Enter any character!") ascii = ord(character) if ascii>=97 and ascii<=122: print("This is a lowercase character!") else: print("This is not a lowercase character!")
Finally, remember that in Python, you can easily access each character of a string using a for loop:
string = "Hello world!" for character in string: print(character)
Alternative method:
string = "Hello world!" for i range in range(0,len(string)): print(string[i])
Your Turn…
It is now your turn to code your own uppercase() function in Python, without using the existing upper() or lower() functions!
Remember, your function will take a string as a parameter and will need to:
- Access each character of the given string
- Find out if the character is a lowercase letter of the alphabet
- If so, convert this character to its uppercase equivalent. (We will do so using the ASCII code!)
- Append the character to a new string
- Return the new string once all character have been checked.
We have started this function for you and you will now need to complete the code to implement the above 5 steps!
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area