Type Here to Get Search Results !

Highlight Search Words,

Highlight Search Words,

Certainly! Here's an example of a function in Python that highlights search words within a given text:

python

Copy code

def highlight_search_words(text, search_words):

    """

    Highlights search words within a given text by wrapping them with HTML span tags. 

    Args:

        text (str): The text in which the search words will be highlighted.

        search_words (list): A list of search words to highlight. 

    Returns:

        str: The text with highlighted search words.

    """

    highlighted_text = text

    for word in search_words:

        highlighted_text = highlighted_text.replace(word, f'<span style="background-color: yellow;">{word}</span>') 

    return highlighted_text 

# Example usage

input_text = "I love coding. Coding is my passion."

search_words = ["coding", "passion"] 

highlighted_output = highlight_search_words(input_text, search_words)

print (highlighted_output)

In this script, the highlight_search_words() function takes the input text and a list of search words as input. It iterates through each search word and replaces occurrences of the word in the text with a highlighted version wrapped in HTML span tags. The span tags are styled with a yellow background color to highlight the search words.

The example usage section demonstrates how to use the function by providing a sample input text and a list of search words. It then prints the text with the highlighted search words. 

When running this script, the occurrences of the search words in the input text will be highlighted with a yellow background color, making them visually stand out.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad-3

Below Post Ad-4

Hollywood Movies-5