title: Replace a character if it only appears at beginning
body: <p>I have a file that has hundreds of lines. I need to read through the entire file and replace a variable name if its name begins with $ character, for example:</p>
  <pre><code>String $number1;
  int $variable2;
  double google$3;
  ......
  </code></pre>
  <p>In this example the code should replace $number1 with Nnumber1 and $variable2 with Nvariable2 so every time there is a $ at the beginning of a variable name it replaces it with an N . However, it should not change the variable name if the $ character does not occur at the beginning. so google$3 remains the same
  This is the code I have</p>
  <pre><code># Read in the file
  with open('filename', 'r') as file :
    data = file.read()
  
  # Replace  string
  data = data.replace('$', 'N')
  
  # Write
  with open('filename', 'w') as file:
    file.write(data)
  </code></pre>
  <p>However the result I'm getting is not the one I want. I'm replacing every occurrence of $ to N I just want the beginning $ to be replaced.</p>
  <pre><code>String Nnumber1;
  int Nvariable2;
  double googleN3;
  </code></pre>
  <p>Edit: There is no guarantee what comes before the $ character it could be anything</p>