I am learning python and in the course, I had to make a translator that transforms vowels into the letter "g".
In the program, I had to check if the phrase to be translated had any uppercase vowels in order to replace them with a capital letter "G".
And I fail to understand why .lower()
doesn't apply for the rest of the code? In my thinking, if I apply letter.lower()
in the next line the value of the variable letter
should still be in lowercase. Here is my code:
def translate(phrase):
translated = ""
for letter in phrase:
if letter.lower() in "aeouiy":
if letter.isupper():
translated = translated + "G"
else:
translated = translated + "g"
else:
translated = translated + letter
return translated
print(translate(input("enter phrase to translate into giraffe elegant language: ")))
The strings
in Python are immutable.
Strings are immutable sequences of Unicode code points.
And the lower() method of str
class in Python doesn't modify the object in-place but it returns a new str object with all of it's characters lower cased.
Lower():
Return a copy of the string with all the cased characters converted to lowercase. For 8-bit strings, this method is locale-dependent.
See this too:
The lowercasing algorithm used is described in section 3.13 of the Unicode Standard.
You will need to reassign your str object to the one returned by lower()
method. Like this:
letter = letter.lower()