Say that the input string is stored in inputs
and that you want to encode it without builtins and string methods?
transl = {'a':'W', ...}
encryp = ''
for e in (transl[c] for c in inputs): encryp += e
If a string method (not applied directly to inputs
) is allowed
transl = {'a':'W', ...}
encryp = ''.join(transl[c] for c in inputs)
In both cases we use a dict
to do the translation, constructing a sequence of encoded characters; to join these characters we can use a for
loop and string concatenation (the +
operator) or, in more idiomatic and efficient way, join them using the null string and its method .join()
Of course, the right way to proceed is using .translate()
, as shown in danidee's answer.