human
Python function to 2 replace text in string.
assistant
Here is a Python function to replace text in string

import re

def replace_text_in_string(string, text1, text2):
    """
    Replace text1 with text2 in string.
    """
    return re.sub(r'(?<!\S)(?=\S)' + text1 + r'(?!\S)(?=\S)', text2, string)

# Enter the string
string = "This is a string"

# Calling the function
print((replace_text_in_string(string, "a", "b")))

#Example Output
# This is a string

#Remember to inspect code for bugs.