"More About Unicode in Python 2 and 3"

Chris Angelico rosuav at gmail.com
Mon Jan 6 11:30:17 EST 2014


On Tue, Jan 7, 2014 at 3:24 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> If you don't want to use the codec, you can do it by hand:
>
> def rot13(astring):
>     result = []
>     for c in astring:
>         i = ord(c)
>         if ord('a') <= i <= ord('m') or ord('A') <= i <= ord('M'):
>             i += 13
>         elif ord('n') <= i <= ord('z') or ord('N') <= i <= ord('Z'):
>             i -= 13
>         result.append(chr(i))
>     return ''.join(result)
>
> But why would you want to do it the slow way?

Eww. I'd much rather use .translate() than that :)

ChrisA



More information about the Python-list mailing list