add mark into sentence if some word in sentence

Question:

I have a table like below

sentence key phase
I love Python love Python
I love Coding love Coding

I want to add the mark $ and /$ in the head and tail of the key phase like below.

sentence key phase
I $love Python/$ love Python
I $love Coding/$ love Coding

Any one know how to do this?

Asked By: Z.L

||

Answers:

What have you tried so far? It’s helpful for others to help you debug.

This may be a starting point to help though

def transform_string(input_string, key_phrase):
    return input_string.replace(key_phrase, f"${key_phrase}/$")

input_string = "I love Python"
key_phrase = "love Python"

transformed_string = transform_string(input_string, key_phrase)
print(transformed_string)
Answered By: Psilly sighbs
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.