Exercise 53

Using Exercise 51, create a python program as a function which takes as a parameter a string s and which returns the same string in lowercase without using the lower() method. You can use the chr() and ord() methods.


def toLowercase(s):
# by using the exercise 51 we can obtain the lists of all upercase characters and lowercase characters:
uppercase_characters = [chr(i) for i in range(65 , 91)]
lowercase_characters = [chr(i) for i in range(97 , 122)]
# then we can obtain a lowercase characters from uppercase characters by adding 32 to its ascii code

# initializing the searched string
s_lower = ""
for x in s:
if x in uppercase_characters:
x = chr(ord(x) + 32)
s_lower = s_lower + x
else :
s_lower = s_lower + x

return s_lower
print(toLowercase("PYTHON COURSES"))
# The output is : python courses

Younes Derfoufi

Leave a Reply