Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Solution: ROT13

  • rot13
  • codecs
  • encoding
import sys

if len(sys.argv) != 2:
    exit(f"Usage: {sys.argv[0]} TEXT")

original = sys.argv[1]

encoded = ''
for char in original:
    code = ord(char)
    if 'a' <= char <= 'z':
    #if ord('a') <= code and code <= ord('z'):
        new_char = chr((code-ord('a') + 13 ) % 26 + ord('a'))
    elif 'A' <= char <= 'Z':
        new_char = chr((code-65 + 13 ) % 26 + 65)
    else:
        new_char = char

    encoded += new_char

print(encoded)

Of course instead of implementing all the calculations by yourself you can also rely on a module that comes with Python:

import sys
import codecs

if len(sys.argv) != 2:
    exit(f"Usage: {sys.argv[0]} TEXT")

original = sys.argv[1]

encoded = codecs.encode(original, encoding='rot_13')

print(encoded)