Answer to Question #347037 in Python for Putsununu

Question #347037

Write a function(preLetterCase) that accepts two parameters ( a string ,and a letter). The function must return the string with all characters before the first instance of the letter converted to lowercase and all other characters converted to uppercase. Hence, if the specified letter is the first character of the string then the entire string will be converted to uppercase. The new string should be returned by the function.


e.g.

  1. preLetterCase ("CAtCHa","a") -> "cATCHA",
  2. preLetterCase ("Preteen","e") -> "prETEEN"
  3. preLetterCase ("You've got this", "m") -> "you've got this"
  4. preLetterCase ("Keep trying", "k") -> "KEEP TRYING"
1
Expert's answer
2022-06-01T12:54:20-0400
def preLetterCase(string, letter):
    for i in range(len(string)):
        if string[i].lower() == letter.lower():
            return string[:i].lower() + string[i:].upper()
    return string


print(preLetterCase ("CAtCHa","a"))
print(preLetterCase ("Preteen","e"))
print(preLetterCase ("You've got this", "m"))
print(preLetterCase ("Keep trying", "k"))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS