title: Use a variable name in re.sub
body: <p>I have a function with which I'm using regular expressions to replace words in sentences. </p>
  
  <p>The function that I have looks as follows:</p>
  
  <pre><code>def replaceName(text, name):
      newText = re.sub(r"\bname\b", "visitor", text) 
      return str(newText)
  </code></pre>
  
  <p>To illustrate:</p>
  
  <pre><code>text = "The sun is shining"
  name = "sun"
  
  print(re.sub((r"\bsun\b", "visitor", "The sun is shining"))
  
  &gt;&gt;&gt; "The visitor is shining"
  </code></pre>
  
  <p>However:</p>
  
  <pre><code>replaceName(text,name)
  &gt;&gt;&gt; "The sun is shining"
  </code></pre>
  
  <p>I think this doesn't work because I'm using the name of a string (name in this case) rather than the string itself. Who knows what I can do so this function works?</p>
  
  <p>I have considered: </p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/18345656/using-variable-for-re-sub">Using variable for re.sub</a>,
  however although the name is similar, its a different question. </li>
  <li><a href="https://stackoverflow.com/questions/18768412/python-use-variable-in-re-sub">Python use variable in re.sub</a>, however this is just about date and time. </li>
  </ol>