Replace special character in string
<p>I'm having a issue with a problem where I want to replace a special character in a text-file that I have created with a string that I defined.</p>

<pre><code>"""
This is a guess the number game.
"""
import datetime, random
from random import randint

fileOpen = open('text.txt','r')
savedData = fileOpen.read()
fileOpen.close()
#print (savedData)

now = datetime.datetime.now()
date = now.strftime("%Y-%m-%d")
date = str(date)
#print(date)
time = now.strftime("%H:%M")
time = str(time)
#print(time)


savedData.replace(";", date)
print (savedData)
</code></pre>

<p>I have tried with something that looks like this. I thought I could just read from the file, save it's content in my own string and then use the <code>replace</code> function to alter the string.
But when I try to do the last print, noting has happened with the <code>saveData</code> string. What am I doing wrong?</p>

<p>The text-file just looks like this, all is on one line:</p>

<pre><code>Today it's the ; and the time is (. I'm feeling ) and : is todays lucky number. The unlucky number of today is #
</code></pre>

<p><a href="https://docs.python.org/3/library/stdtypes.html#str.replace" rel="nofollow"><code>str.replace</code></a> doesn't alter the string in place, no string operations do because <em>strings are immutable</em>. It returns a copy of the replaced string which you need to assign to another name:</p>

<pre><code>replacedData = savedData.replace(";", date)
</code></pre>

<p>now your replaced string will be saved to the new name you specified in the assignment.</p>

python string python-3.x