sentence = "The quick brown fox jumps over the lazy dog."
for word in sentence:
if word.startswith("f" or "F"):
uppercase_text = sentence.replace(word, str(word.upper()))
print(uppercase_text)
if word.endswith("e" or "E"):
e_text = sentence.replace(word, str("my"))
print(e_text)
My program is supposed to replace every word in the string that ends with the letter "e" with the word "my." I am also trying to convert every word that ends with the letter "f" into uppercase. However, only the first letter is being capitalized instead of the entire word. And every letter "e" is getting replaced with "my" and still leaves the rest of the word intact, instead of replacing the whole word with the word "my."
Thmy quick brown fox jumps ovmyr thmy lazy dog.
The quick brown Fox jumps over the lazy dog.
Thmy quick brown fox jumps ovmyr thmy lazy dog.
Thmy quick brown fox jumps ovmyr thmy lazy dog.
My output looks like this. My desired output would look like "My quick brown FOX jumps over my lazy dog," and this new string would only print once. I'm not sure why the whole word is not getting replaced / capitalized.
I also wasn't sure how to combine the code for uppercase_text and e_text so the sentence only prints once. I'm aware it's printing twice right now because those are two separate variables. Not sure why it's getting printed four times though, and I don't know how to fix that either.
I've tried making variables for the text that is supposed to be manipulated, like this:
ends_with_e = sentence.endswith("e" or "E")
for ends_with_e in sentence:
e_text = sentence.replace(word, str("my"))
print(e_text)
But this prints even more nonsensical output:
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
The quick brown fox jumps over the lazy dogmy
I can see what's going on here - the program is just printing "my" at the end of the string, but I can't tell how to fix this either.
Thanks to anyone who has any insight on this!