擅长:python、mysql、java
<pre><code>>>> strs="this is a message"
>>> strs="".join(['a' if x=='e' else 'e' if x=='a' else '3' if x=='s' else x for x in strs])
>>> print(strs)
thi3 i3 e ma33ega
</code></pre>
<p><strong>或者按照罗伯特的建议,使用字典</p>
<pre><code>>>> strs="this is a message"
>>> dic={'a':'e','e':'a','s':'3'}
>>> strs="".join((dic.get(x,x) for x in strs))
>>> print(strs)
thi3 i3 e ma33ega
</code></pre>
<p><strong>或:</strong></p>
<pre><code>>>> strs="this is a message"
>>> dic={'a':'e','e':'a','s':'3'}
>>> new_strs=''
>>> for x in strs:
if x in dic:
new_strs += dic[x]
else:
new_strs += x
>>> print(new_strs)
thi3 i3 e ma33ega
</code></pre>