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"))
@DEVTOSHPALEI
Copy link

I got the Output here:

Have a NICE day
Shhh, don't be so LOUD!
Automating with Python is FUN

@rahulgpt2202
Copy link

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))

@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