title: Spacy replace token
body: <p>I am trying to replace a word without destroying the space structure in the sentence. Suppose I have the sentence <code>text = &quot;Hi this is my dog.&quot;</code>. And I wish to replace dog with <code>Simba</code>. Following the answer from <a href="https://stackoverflow.com/a/57206316/2530674">https://stackoverflow.com/a/57206316/2530674</a> I did:</p>
  <pre class="lang-py prettyprint-override"><code>import spacy
  nlp = spacy.load(&quot;en_core_web_lg&quot;)
  from spacy.tokens import Doc
  
  doc1 = nlp(&quot;Hi this is my dog.&quot;)
  new_words = [token.text if token.text!=&quot;dog&quot; else &quot;Simba&quot; for token in doc1]
  Doc(doc1.vocab, words=new_words)
  # Hi this is my Simba . 
  </code></pre>
  <p>Notice how there was an extra space at the end before the full stop (it ought to be <code>Hi this is my Simba.</code>). Is there a way to remove this behaviour. Happy for a general python string processing answer too.</p>