VukMC
VukMC

Reputation: 29

How to highlight words in a string [Python]

I want to make a function that will highlight [ .upper() ] every occurrence of any of the words given.

I understand why it does that but I don't know any other way to even be remotely close to making it work so your help would really help me! 😊 I tried saving different final results into a list but then how am I supposed to connect them into 1 final final sentence?

def highlight_words(sentence, words):
    final = ""
    k = 0
    for j in range(len(words)):
        for i in range(len(sentence)):
            if k != 0:
                k -= 1
                continue
            changed = ""
            if sentence.lower().startswith(words[j].lower(), i):
                changed = sentence[i:i+len(words[j])].upper()
                final += changed
                k = len(words[j]) - 1
            else:
                final += sentence[i]
    return final

print(highlight_words("Have a nIcE day, you Nice person!!", ["nice"]))
print(highlight_words("Shhh, don't be so loud!", ["loud", "Be"]))
print(highlight_words("Automating with Python is fun", ["fun", "auTomaTiNG"]))

This is what the program prints:

Have a NICE day, you NICE person!!

Shhh, don't be so LOUD!Shhh, don't BE so loud!

Automating with Python is FUNAUTOMATING with Python is fun

I'd appreciate if you don't use any imported libraries in your solutions! Thanks in advance!

Upvotes: 0

Views: 9438

Answers (5)

Sophie
Sophie

Reputation: 51

This problem can be solved with string.replace(old, new) method.

Here is my solution:

def highlight_word(sentence, word):
    return sentence.replace(word, word.upper())

Upvotes: 5

Mohammad Bilal
Mohammad Bilal

Reputation: 3

This is my answer and it works fine from Coursera Quiz.

def highlight_word(sentence, word):
    ans=""
    parts=sentence.split()#parts = splitted sentence
    for part in parts:
        Cpart=part.strip("!,")#Cpart means Clean part after stripping the ! ans , etc
        if Cpart==word:#if our clean part is equal to word provided
            part=part.upper()#then convert the part to Upper Case
        ans = ans+" "+part#keep adding the parts in ans string
    return(ans)

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

Upvotes: 0

Paul Menzies
Paul Menzies

Reputation: 161

Using recursion:-

def highlight_words(sentence, words):
    for word in words:
        pos = sentence.lower().find(word.lower())
        sentence = sentence if pos < 0 else sentence[0:pos] + word.upper() + highlight_words(
            sentence[pos + len(word):], [word])
    return sentence

Upvotes: 0

TheManWithNoPlan
TheManWithNoPlan

Reputation: 436

I think it is much easier if you make the outer loop the string instead of the words. One option is below.

def highlight_words(sentence, words):
    for i in range(len(sentence)):
        for j in range(len(words)):    
            if sentence.lower().startswith(words[j].lower(), i):
                sentence = sentence[:i] + sentence[i:i+len(words[j])].upper() + sentence[i+len(words[j]):]
    return sentence

print(highlight_words("Have a nIcE day, you Nice person!!", ["nice"]))
print(highlight_words("Shhh, don't be so loud!", ["loud", "Be"]))
print(highlight_words("Automating with Python is fun", ["fun", "auTomaTiNG"]))

Upvotes: 1

Sahith Kurapati
Sahith Kurapati

Reputation: 1715

You can do it simply by using split and join like so:

def highlight_words(sentence, words):
    for word in words:
        if(word.lower() in sentence.lower()):
            sentence = sentence.split(word)
            sentence = word.upper().join(sentence)
    return sentence

Hope it helps :)

Upvotes: 1

Related Questions