Last active
December 27, 2022 17:03
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)