title: Python add space
body: <p>We have the repetitive words like Mr and Mrs in a text. We would like to add a space before and after the keywords Mr and Mrs. But, the word Mr is getting repetitive in Mrs. Please assist in solving the query:</p>
  
  <p>Input:</p>
  
  <p>Hi This is Mr.Sam. Hello, this is MrsPamela.Mr.Sam, what is your call about? Mrs.Pamela, I have a question for you.</p>
  
  <pre><code>import re
  
  s = "Hi This is Mr Sam. Hello, this is Mrs.Pamela.Mr.Sam, what is your call about? Mrs. Pamela, I have a question for you."
  words = ("Mr", "Mrs")
  
  
  def add_spaces(string, words):
  
  for word in words:
      # pattern to match any non-space char before the word
      patt1 = re.compile('\S{}'.format(word))
  
      matches = re.findall(patt1, string)
      for match in matches:
          non_space_char = match[0]
          string = string.replace(match, '{} {}'.format(non_space_char, word))
  
      # pattern to match any non-space char after the word
      patt2 = re.compile('{}\S'.format(word))
      matches = re.findall(patt2, string)
      for match in matches:
          non_space_char = match[-1]
          string = string.replace(match, '{} {}'.format(word, non_space_char))
  
  return string
  
  
  print(add_spaces(s, words))
  </code></pre>
  
  <p><strong>Present Output:</strong></p>
  
  <pre><code>Hi This is Mr .Sam. Hello, this is Mr sPamela. Mr .Sam, what is your call about? Mr s.Pamela, I have a question for you.
  </code></pre>
  
  <p><strong>Expected Output:</strong></p>
  
  <pre><code>Hi This is Mr .Sam. Hello, this is Mrs Pamela. Mr .Sam, what is your call about? Mrs .Pamela, I have a question for you.
  </code></pre>