Posted By

Valdemarick on 02/28/09


Tagged

python encode decode rot rot3 rotn


Versions (?)

Flexible Rot13 Function


 / Published in: Python
 

This function takes 2 arguments, first is the string to be encoded (or decoded) the second is optional and can be used to change the rotation amount to something other than 13.

  1. def rot(s, n=13):
  2. r = ""
  3. for c in s:
  4. cc = c
  5. if cc.isalpha():
  6. cc = cc.lower()
  7. o = ord(cc)
  8. ro = (o+n) % 122
  9. if ro == 0: ro = 122
  10. if ro < 97: ro += 96
  11. cc = chr(ro)
  12. r = ''.join((r,cc))
  13. return r

Report this snippet  

You need to login to post a comment.