Search code examples
pythonstringreplacestartswithends-with

How can I replace text that starts or ends with a certain letter in Python?


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!


Solution

  • Your code will parse the string by character, not by word.

    sentence = "The quick brown fox jumps over the lazy dog."
    
    for word in sentence: # By character, not word 
    ...
    

    Use .split() to create an array delimited by whitespace.

    https://www.w3schools.com/python/ref_string_split.asp

    sentence = "The quick brown fox jumps over the lazy dog."
    
    for word in sentence.split():  # Creates an array of words  
    ...
    

    In your logic you are calling sentence.replace(...) which means you're looking through the entire input sentence but since you're using a loop, you should only handle each word at a time.

    Use instead:

    sentence = "The quick brown fox jumps over the lazy dog."
    result = []  # Create result array since the `sentence` array is immutable 
    
    for word in sentence.split():
        word_result = word
    
        if word.startswith("f" or "F"):
            word_result = word.upper()
        if word.endswith("e" or "E"):
            word_result = "my"
    
        result.append(word_result)
    
    print(" ".join(result)) # Print once with space reinserted 
    

    You are also printing for each "pass" of the sentence so that's why you have multiple lines. Print only once at the end.

    This doesn't handle if both cases are met like words such as "fire".