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")) |
def combine_lists(list1, list2):
Generate a new list containing the elements of list2
Followed by the elements of list1 in reverse order
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Jamies_list, Drews_list))
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
I got the Output here:
Have a NICE day
Shhh, don't be so LOUD!
Automating with Python is FUN