Skip to content

Instantly share code, notes, and snippets.

@AnisahTiaraPratiwi
Last active December 27, 2022 17:03
Show Gist options
  • Save AnisahTiaraPratiwi/59b5bf1d51896b99130de3154eb1cb27 to your computer and use it in GitHub Desktop.
Save AnisahTiaraPratiwi/59b5bf1d51896b99130de3154eb1cb27 to your computer and use it in GitHub Desktop.
Question 2 The highlight_word function changes the given word in a sentence to its upper-case version. For example, highlight_word("Have a nice day", "nice") returns "Have a NICE day". Can you write this function in just one line?
def highlight_word(sentence, word):
return(sentence.replace(word,word.upper()))
print(highlight_word("Have a nice day", "nice"))
print(highlight_word("Shhh, don't be so loud!", "loud"))
print(highlight_word("Automating with Python is fun", "fun"))
@h8boiman
Copy link

def highlight_word(sentence, word):
index_of_word=sentence.index(word)
lenght_of_word=len(word)
new=sentence[0:i]+word.upper()+sentence[index_of_word+lenght_of_word:]
return "".join(new)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment