Flexible Rot13 Function


/ Published in: Python
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments


You need to login to view comments.