Search code examples
pythonstringfor-loopiterationlowercase

python making my own lower case converter


I am new to programming and I need some help for an free online tutorial to learn Python. I am building my own method to convert a an input string to all lower cases. I cannot use the string.lower() method. In the code I have so far I cannot figure out how to separate the input string into characters that can be inputed into my character converter lowerChar(char).

string=input #input string


def lowerChar(char):              #function for converting characters into lowercase
   if ord(char) >= ord('A') and ord(char)<=ord('Z'):
      return chr(ord(char) + 32)
   else:
      return char

def lowerString(string):     #function for feeding characters of string into lowerChar
   result = ""
  for i in string:
     result = result + lowerChar(string[i])
     return result

Solution

  • You are really close:

    def lowerString(string):
      result = ""
      for i in string:
         # i is a character in the string
         result = result + lowerChar(i)
      # This shouldn't be under the for loop
      return result
    

    Strings are iterable just like lists!

    Also, make sure to be careful about your indentation levels, and the number of spaces you use should be consistent.