Richard Modad
Richard Modad

Reputation: 202

Capitalize a word based on a matching word in a list

Working through a "Coursera Python" course and I am having a lot of trouble.

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". I want help to rewrite this function in just one line?

def highlight_word(sentence, word):
    return(___)

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

I think I can do this in a larger statement but does anyone know how to return this correctly in a single line? I am guessing it will involve a list comprehension.

Upvotes: 2

Views: 31526

Answers (7)

user3300327
user3300327

Reputation: 17

Based on what the course teaches, the below expression works for me: def highlight_word(sentence, word): return(sentence[:sentence.find(word)]+word.upper()+sentence[sentence.find(word)+len(wor d):])

In this I have used string slicing only

Upvotes: 1

Muhammad Khan
Muhammad Khan

Reputation: 45

You can do this in one line by using 'replace(string,replaced_string)' as shown:

def highlight_word(sentence, word):
return(sentence.replace(sentence[sentence.find(word):sentence.find(word)+len(word)],sentence[sentence.find(word):sentence.find(word)+len(word)].upper()))

sentence.replace(sentence[sentence.find(word):sentence.find(word)+len(word)]

and it works for the following inputs:

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

You get the following out-put

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

Upvotes: 0

Archie Tripathi
Archie Tripathi

Reputation: 21

You can try this one, it worked for me !

def highlight_word(sentence, word):
  return sentence[0:sentence.index(word)] + word.upper()+ sentence[sentence.index(word)+len(word):] 

Upvotes: 2

Ben S
Ben S

Reputation: 115

A partial answer in one line, in the way that @Barmar hinted at:

def highlight_word(sentence, word): return " ".join([x.upper() if x.lower() == word.lower() else x for x in sentence.split()])

Basically - split the sentence into words, and use list comprehension to upper() the matching word. Then use join() to bring the sentence back together.

Edit: sentence.split() will split only on whitespace, so it won't capitalize the second example as "loud!" != "loud". In this case, you could use the regex library to do a substitution.

Yes it works : enter image description here

Upvotes: 2

Richard Modad
Richard Modad

Reputation: 202

The re.sub works but it was still the incorrect answer and overly complicated - @C. Leconte was correct to use a simple replace.

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

Thanks

Upvotes: 12

Omer Tekbiyik
Omer Tekbiyik

Reputation: 4744

You can do it with re.sub() functions

import re

def highlight_word(sentence, word):
     return [(re.sub(y, y.upper(), x)) for x in sentence for y in word if y in x]


words = ["have a nice day", "Shhh, don't be so loud!","Automating with Python is fun"]
lowers = ['nice','loud','fun']

print(highlight_word(words,lowers))

OUTPUT :

['have a NICE day', "Shhh, don't be so LOUD!", 'Automating with Python is FUN']

Upvotes: 0

DarrylG
DarrylG

Reputation: 17156

Can be done with a regular expression using re.sub

def highlight_word(sentence, word):
  return re.sub(r'\b' + word + r'\b', word.upper(), sentence)

Upvotes: 2

Related Questions