Adding asterisk before and after a particular word in a sentence in python

When working with text in Python, it is common to come across situations where you need to add special characters or formatting to specific words or phrases within a sentence. One such requirement is to add asterisks before and after a particular word in a sentence. In this article, we will explore three different ways to achieve this in Python.

Option 1: Using String Manipulation

The first option involves using basic string manipulation techniques to achieve the desired result. We can split the sentence into individual words, iterate over them, and add asterisks before and after the target word. Here’s an example:

sentence = "This is a sample sentence"
target_word = "sample"

words = sentence.split()
for i in range(len(words)):
    if words[i] == target_word:
        words[i] = "*" + words[i] + "*"

modified_sentence = " ".join(words)
print(modified_sentence)

This approach splits the sentence into a list of words using the split() method. It then iterates over each word and checks if it matches the target word. If a match is found, asterisks are added before and after the word. Finally, the modified words are joined back into a sentence using the join() method.

Option 2: Using Regular Expressions

If you are familiar with regular expressions, you can leverage their power to solve this problem. Regular expressions provide a concise and flexible way to match patterns within text. Here’s an example of using regular expressions to add asterisks:

import re

sentence = "This is a sample sentence"
target_word = "sample"

modified_sentence = re.sub(r'b' + target_word + r'b', r'*g<0>*', sentence)
print(modified_sentence)

In this approach, we use the re.sub() function to substitute the target word with the same word surrounded by asterisks. The regular expression pattern b ensures that the target word is matched as a whole word and not as part of another word.

Option 3: Using f-strings

If you are using Python 3.6 or above, you can take advantage of f-strings, which provide a concise and readable way to format strings. Here’s an example of using f-strings to add asterisks:

sentence = "This is a sample sentence"
target_word = "sample"

modified_sentence = sentence.replace(target_word, f"*{target_word}*")
print(modified_sentence)

In this approach, we use the replace() method to replace the target word with the same word surrounded by asterisks. The f-string syntax allows us to easily include the target word within the replacement string.

After exploring these three options, it is clear that the third option using f-strings is the most concise and readable solution. It requires fewer lines of code and leverages the built-in string method, making it the preferred choice for adding asterisks before and after a particular word in a sentence in Python.

Rate this post

13 Responses

  1. Option 1: String Manipulation seems like a tedious way to add asterisks. Why not keep it simple with Option 3: f-strings?

    1. I have to disagree. While f-strings might be cool, adding asterisks shouldnt be our top priority. Lets focus on the substance of our code rather than flashy decorations. Prioritize functionality over aesthetics.

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents