EDIT: this is good, thanks to Ashwini Chaudhary
>>> d = {'(': 'a'}
>>> "".join(d.get(x,x) for x in 'text with m(ny letters')
'text with many letters'
This is a longer solution but does the equivalent:
So you want to translate letters and keep thoos untouched that you do not know?
>>> d = {'(': 'a'}
>>> ''.join(map(lambda letter: d.get(letter, letter), 'text with m(ny letters'))
'text with many letters'
Explanation
This
d.get(letter, letter)
tries to find the letter in d and if not present returns letter (the second letter)
lambda letter: lalala
is the same as
def f(letter):
return lalala
f # return f
map(function, list)
does this:
[function(element) for element in list]
which is a short form of
l = []
for element in list:
l.append(function(element))
l # returns that list
''.join(list)
is the same as
string = ''
for element in list:
string += element
string # return string as result