Rot 13 SimpleExplanation

How to use ROT13 in Python? Simply Explained

ROT13 is a simple encryption method that shifts the string x (clear text) by 13 positions in the alphabet.

This simple Python one-liner does ROT13 encryption for you:

abc = "abcdefghijklmnopqrstuvwxyz"
"".join([abc[(abc.find(c)+13)%26] for c in x])

This Python library call does ROT13 encryption in Python:

import codecs
codecs.encode(phrase, 'rot_13')


(Reading time — 12 minutes, or watch the video!)

ROT13 Video Tutorial

What is ROT13?

The ROT13 algorithm is a simple encryption algorithm. It is used in many forums (for example Reddit) to prevent spoilers – or to hide the semantics of a conversation from newbies. (Please find a separate ROT13 application section below.)

ROT13 is so simple that it practically provides no security. But it helps you to finally understand these insider conversations on Reddit.

The algorithm can be explained in one sentence. ROT13 = Rotate the string to be encrypted by 13 positions (modulo 26) in the alphabet of 26 characters.


In other words, you want to encrypt a string. You shift each character by 13 positions in the alphabet. When shooting over the last character “z”, you start over at the first position in the alphabet “a”.

What are ROT13 implementations in Python?

I will first give you a simple Python implementation of the ROT13 algorithm that is easily understandable. Then, I provide you a short and concise Python one-liner. Finally, I give you the library call for ROT13 encryption. Take the version you prefer.

So check out this ROT13 algorithm in very simple Python code without using libraries. Read the code carefully, because I will ask you a question about the code in a moment.

def rot13(phrase):
   abc = "abcdefghijklmnopqrstuvwxyz"
   out_phrase = ""

   for char in phrase:
       out_phrase += abc[(abc.find(char)+13)%26]
   return out_phrase

phrase = "xthexrussiansxarexcoming"

# kgurkehffvnafknerkpbzvat

# What's the output?

The last print statement showcases a nice property of the algorithm. Which one?

The solution to the puzzle is the output “thexrussiansxarexcoming” because the function rot13 is its own inverse function (shifting by 13+13 positions brings you back to the original output).

An advanced coder will always prefer the shortest and cleanest way of writing Pythonic code. So, let’s rewrite the ROT13 algorithm as a Python one-liner.

abc = "abcdefghijklmnopqrstuvwxyz"

def rt13(x):
   return "".join([abc[(abc.find(c)+13)%26] for c in x])


We create a list of encrypted characters and join this list to the final result. The code uses list comprehension to create the list. It encrypts each character from the original string x separately. How? Simply by replacing it with the character 13 positions on the right in the alphabet.

Note that it is crucial to prevent overshooting for all characters in the alphabet with index>13. To do this, we use the modulo operator to ensure that we restart on the left with index 0.

To encrypt character ‘z’, the algorithm shifts its index 25 by 13 index positions to the right and takes the result modulo 26 to get the final index of the encrypted character. Essentially, this prevents overshooting by restarting the shift operation at index 0 which results in the following shift sequence: 25 > 0 > 1 > … > 12.

Alternative solution: one of my “Coffee Break Python” email subscribers, Thomas, came up with an alternative solution that is fast and easy to read, too.

def rot13(phrase):
    key = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    val = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
    transform = dict(zip(key, val))
    return ''.join(transform.get(char, char) for char in phrase)

The idea is to “hardcode” the mapping between keys and values which is a bit more “tedious” programming work but is still a perfectly valid solution to the ROT13 algorithm (and it works for Uppercase, too).

Is there a library for ROT13 in Python?

Yes! The library is called ‘codecs’. Using the ROT13 algorithm with the library is as simple as importing the library via ‘import codecs’ and executing the ‘encode(…)’ function.

Here is an example:

import codecs

phrase = "xthexrussiansxarexcoming"

print(codecs.encode(codecs.encode(phrase, 'rot_13'), 'rot_13'))
# xthexrussiansxarexcoming

print(codecs.encode('hello', 'rot_13'))
# uryyb

The encode function of the codecs library takes up to three parameters. The first parameter is the string object to encode. The second parameter is the encoding scheme (default: ‘utf-8’). The third parameter allows you to customize the error handling. In most cases, you can skip the last parameter and use the default error handling.

What are the applications of the ROT13 algorithm?

The ROT13 algorithm is easy to decrypt. An attacker can easily crack your code by running a probabilistic analysis on the distribution of the letters in your encrypted text. You should never rely on this algorithm to encrypt your messages!

Then, what are the applications of the ROT13 algorithm?

  •    Obscure potentially offensive jokes in online forums.
  •    Obscure the result of puzzles in online forums.
  •    Obscure possible spoilers for movies or books.
  •    Make fun of existing (weak) encryption algorithms: “56-bit DES is stronger than ROT13”
  •    Obscure email addresses on websites against not very sophisticated email bots (the 99%).
  •    Use it as a game to find phrases that make sense in both forms, encrypted or decrypted. Examples: (png, cat), (be, or).
  •    ROT13 is a special case of the popular Caesar cipher. ROT13 serves as an educational tool to explain it. (example)

In summary, ROT13 is more a fun encryption method that has been a popular running gag in the internet culture (in the good old days)…

As a user on StackExchange describes it:

“So a lot of things that did mild obfuscation used ROT13, because it was commonly available, and so it’s been backported into a number of more modern languages. It’s just a weird quirk.”

How is CAPITALIZATION handled (uppercase and lowercase)?

The encode function of the codecs library handles capitalization for you. If you apply ROT13 to an uppercase letter, it will remain uppercase after the encoding. If you apply ROT13 to a lowercase letter, it will remain lowercase.

Here is an example:

import codecs

print(codecs.encode('Hello', 'rot_13'))
# Uryyb

How is ROT13 related to the Caesar cipher?

The Caesar cipher is the generalization of the ROT13 algorithm. So what’s the Caesar cipher?

“It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet.”


As you can see, ROT13 does nothing but fixing the “number of positions down the alphabet” to +13.

Why shifting the cleartext (also called ‘plaintext’) by exactly 13 positions? This ensures that applying the encryption two times leads to the original clear text. Hence, you do not have to define two separate methods for encryption and decryption – one method to rule them all.

This is not the case, if you use – for example – ROT5 where you shift the clear text only by 5 positions. If you apply ROT5 twice to the same clear text, you get ROT10 encryption (5+5=10).

Online tool for ROT13 encryption and decryption

To encrypt your own cleartext, simply replace the string value of variable ‘clear_text’ with your personal string.

Click to visit the interactive tool to obfuscate your own texts with ROT13.

What are the alternatives to ROT13?

Most alternatives are stronger than ROT13. Here are a few of them:

  • Triple DES
  • RSA
  • Blowfish
  • Twofish
  • AES

If you want to dive deeper into these alternatives, check out this article that briefly describes their ideas.

What are examples of ROT13?

Here are examples from various sources on the web. I tried to focus on examples where the ROT13 encryption produces some kind of English word as well.

  • aha ↔ nun
  • ant ↔ nag
  • balk ↔ onyx
  • bar ↔ one
  • barf ↔ ones
  • be ↔ or
  • bin ↔ ova
  • ebbs ↔ roof
  • envy ↔ rail
  • er ↔ re
  • errs ↔ reef
  • flap ↔ sync
  • fur ↔ she
  • gel ↔ try
  • gnat ↔ tang
  • irk ↔ vex
  • clerk ↔ pyrex
  • purely ↔ cheryl
  • PNG ↔ cat
  • SHA ↔ fun
  • furby ↔ sheol
  • terra ↔ green
  • what ↔ Jung
  • URL ↔ hey
  • purpura ↔ Chechen
  • shone ↔ FUBAR
  • Ares ↔ Nerf
  • abjurer ↔ nowhere

Where to go from here?

ROT13 is a simple encryption method that shifts the string x (clear text) by 13 positions in the alphabet.

It offers no encryption, only obfuscation. However, it can be a great and fun way to obscure messages in online forums and private communications. ROT13 is a special variant of Caesar’s cipher that allows for the inverse mapping too:

'a' >> (shift by 13 positions) >> 'n' >> (shift by 13 positions) >> 'a'

Do you want to improve your Python skills to the point where every software company would love to hire you because you belong to the top coders? Check out the Coffee Break Python book series! It’s a fun way of accelerating your Python coding skills in a very engaging manner. (And we just reached LeanPub bestseller status in the category Python!)