Capitalize a word based on a matching word in a list
Solution 1
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
Solution 2
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):]
Solution 3
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)
Solution 4
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.
Solution 5
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
Related videos on Youtube
Richard Modad
Updated on September 02, 2020Comments
-
Richard Modad over 3 years
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.
-
Barmar over 4 yearsYou can do it with a simple regular expression replacement.
-
Barmar over 4 yearsYou can do it with
split()
,join()
, and list comprehension, but it will be long and confusing. -
Barmar over 4 years@AndrejKesely That won't respect word boundaries.
-
juanpa.arrivillaga over 4 yearsFor this simple case (ignoring word boundaries) you could probably do
return sentence.replace(word, word.upper())
-
-
Barmar over 4 yearsProbably should use
if x.lower() == word
, so it will work for things likePython
. -
juanpa.arrivillaga over 4 yearsdon't do this:
highlight = lambda
-
Richard Modad over 4 yearsI cannot get that second example working - I assume they messed up when they made the question because regex hasn't been covered whatsoever yet. Is there a way to check for .isalpha() or something within this statement to look beyond the ! attached? Could you please show me what the example might be like using regex?
-
awasi over 4 yearsWhy don't you use a simple replace() method on the string to change word to word.upper(), is that forbidden for you?
-
clover over 3 yearsI have a question. Does anyone know why my code which is similar to the one above wouldn't work? I thought the if condition has to be at the end of the list comprehension statement.
return " ".join([x.upper() for x in sentence.split() if x == word else x])