How to convert Multiplication sign into Asterisk sign in python?
<p>I have a math solver python program but it can't read "Ã" sign, so It can not solve the equation. Is there any way to convert "Ã" into "*"?</p>

<p><strong>Python shell:</strong></p>

<pre><code>&gt;&gt;&gt; 3 Ã 5
`SyntaxError: invalid character in identifier
&gt;&gt;&gt; 3 * 5
15
&gt;&gt;&gt; 
</code></pre>

<p><a href="http://i.stack.imgur.com/fZKi2.png" rel="nofollow">screenshot</a></p>

<p><strong>Update:</strong>
I tried this too, but did not work.</p>

<pre><code> if (parser.getPayload().lower()[:6]=="solve:"):
                    if ("Ã" in parser.getPayload().lower()):
                        str.replace("Ã","*")
                        parser.sendGroupMessage(parser.getTargetID(), str(ast.literal_eval(parser.getPayload()[7:])))
</code></pre>

<p>Let's go over your code line by line.</p>

<pre><code>if (parser.getPayload().lower()[:6]=="solve:"):
</code></pre>

<p>Fine I guess, don't know your protocol but makes sense this way.</p>

<pre><code>                    if ("Ã" in parser.getPayload().lower()):
</code></pre>

<p>Converting to lower case should not be needed. If it were, caching that might be useful. I also wonder whether you need that check, or whether you might as well do the replacement unconditionally, since that makes the code easier and should't be much of a performance problem. It might even help performance since you avoid scanning for the first <code>Ã</code> twice.</p>

<pre><code>                        str.replace("Ã","*")
</code></pre>

<p><a href="https://docs.python.org/3/library/stdtypes.html#str.replace" rel="nofollow"><code>str.replace</code></a> is a method of the class <code>str</code>, not a function in some module called <code>str</code>. You need to call the <code>replace</code> method on your payload. So either use something like <code>parser.setPayload(parser.getPayload().replace("Ã","*"))</code> or store the payload in a local variable which you can modify.</p>

<pre><code>                        parser.sendGroupMessage(parser.getTargetID(), str(ast.literal_eval(parser.getPayload()[7:])))
</code></pre>

<p>Are you sure you only want to do this in the case there is a <code>Ã</code> in the input? Anyway, read <a href="https://docs.python.org/3/library/ast.html#ast.literal_eval" rel="nofollow">the docs for <code>ast.literal_eval</code></a>: it will not evaluate operators. So this is not what you need. If you need to evaluate operators, you may call <code>eval</code>, but if you do, you <strong>must</strong> sanitize your input first to ensure it doesn't do anything evil.</p>

<p>I'd do something like this:</p>

<pre><code>sanitizeRegexp = re.compile(r"\A[0-9+\-*/ ]*\Z") # only allow these
payload = parser.getPayload()
if payload.lower()[:6] == "solve:":
    payload = payload[7:] # strip command
    payload = payload.replace("Ã", "*")
    if sanitizeRegexp.match(payload):
        result = str(eval(payload))
    else:
        result = "Invalid input: " + payload
    parser.sendGroupMessage(parser.getTargetID(), result)
</code></pre>

python math character-encoding special-characters identifier