In Python, decode ROT13 with codecs.decode(text, "rot_13"); the same operation encodes because applying it twice restores the source text.
That standard-library call is a standard-library option when you already have ROT13 text in a Python variable. It requires no third-party package and returns a new string without modifying the original variable.
ROT13 is a fixed substitution, not encryption. It is useful for casual text transformations, CTF exercises, and testing how a program handles encoded input. It should not protect passwords, tokens, private messages, or other secrets.

What Python's rot_13 codec changes
Python's official codecs documentation identifies rot_13 as a string-to-string text transform. The CPython rot_13 codec source provides the direct alphabet-table implementation behind that behavior.
The mapping is intentionally limited to the two English ASCII alphabets. Uppercase letters remain uppercase, lowercase letters remain lowercase, and characters outside those ranges pass through unchanged.
| Input content | Behavior after ROT13 |
|---|---|
| ASCII uppercase A–Z | Rotated by 13 positions while remaining uppercase |
| ASCII lowercase a–z | Rotated by 13 positions while remaining lowercase |
| Digits and punctuation | Unchanged |
| Spaces, tabs, and line breaks | Unchanged |
| Accented letters, Greek, Cyrillic, Arabic, CJK, and emoji | Unchanged |
For example, A maps to N and N maps back to A. Lowercase letters use the same fixed rotation independently, so a maps to n and n maps back to a. This narrow scope also means that a string such as “café” changes its ASCII letters but leaves the accented é untouched.
Decode ROT13 with a Python script
-
Open a Python 3 script or interactive session and place the encoded string in a variable named encoded_text.
-
Import the standard-library codecs module and pass the variable to codecs.decode with the rot_13 codec name.
-
Print the returned string to inspect the decoded result.
-
Run the script with python decode_rot13.py, or execute the lines directly in a Python REPL.
-
To encode text instead, call the same operation on the original string. ROT13 is self-inverse, so decoding and encoding use the same transformation.
A complete script can use the following lines:
import codecs
encoded_text = "Uryyb"
decoded_text = codecs.decode(encoded_text, "rot_13")
print(decoded_text)
The example prints “Hello”; replace the sample value with the text you want to decode. Because the operation restores the source after two applications, the same code also handles encoding.
For a UTF-8 text file, decode the file into a Python string before applying ROT13:
from pathlib import Path
source = Path("encoded.txt").read_text(encoding="utf-8")
decoded = codecs.decode(source, "rot_13")
Path("decoded.txt").write_text(decoded, encoding="utf-8")
The source file's character encoding must be chosen correctly. The ROT13 codec then changes the ASCII letters while retaining the other characters in the decoded string.
Choose between the main Python implementations
Python offers several workable approaches, but they differ in how much of the mapping you define yourself.
| Approach | Character scope | Best fit |
|---|---|---|
| codecs.decode | Uses the standard rot_13 ASCII mapping | Scripts using Python's standard rot_13 codec |
| str.translate | Changes only characters included in your translation table | Code that needs an explicit or reusable mapping |
| Manual function | Depends on the range checks in your function | Learning, testing, or strict control over ASCII scope |
| Browser ROT13 tool | Uses the same ASCII-only ROT13 mapping | Quick checks without installing or running Python |
To use str.translate, build the table from two explicit alphabet strings:
source = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
target = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"
table = str.maketrans(source, target)
decoded = encoded_text.translate(table)
Characters that do not appear in the table are left alone. This approach makes the supported scope visible, although the standard codecs.decode call is usually easier to maintain.
Build a strict ASCII-only decoder
A manual implementation is useful when you want to see the algorithm or enforce the ASCII boundary explicitly. This function preserves case and appends every unmatched character unchanged:
def rot13(text):
result = []
for char in text:
if "A" <= char <= "Z":
value = (ord(char) - ord("A") + 13) % 26 + ord("A")
result.append(chr(value))
elif "a" <= char <= "z":
value = (ord(char) - ord("a") + 13) % 26 + ord("a")
result.append(chr(value))
else:
result.append(char)
return "".join(result)
The uppercase and lowercase branches use separate bases, so the function never changes case. Digits, punctuation, whitespace, NUL characters, combining marks, accented letters, and emoji all enter the final branch and remain unchanged.
One common mistake is to test characters with char.isalpha(). That method recognizes letters from many writing systems, not just A–Z and a–z. An explicit range check avoids accidentally transforming accented Latin letters, Greek, Cyrillic, or other Unicode letters.
Check ROT13 output in a browser
For a no-install workflow, use the ROT13 Encoder Decoder to apply the same ASCII-only mapping in the current browser tab.
-
Paste text containing ASCII letters and any other characters you need to preserve.
-
Select Apply ROT13.
-
Review the transformed output and the reported count of changed ASCII letters.
-
Copy the result for use elsewhere, or apply ROT13 to that result again to recover the original text.
The tool rejects empty input. It accepts non-empty text up to 1,000,000 UTF-16 code units, accepts input at that exact boundary, and rejects one code unit beyond it with an explicit message rather than slicing or silently truncating the text. Non-empty content with no ASCII letters is valid and produces a changed-letter count of zero. NUL is treated as ordinary string data rather than a terminator.
The output has the same length as the input in UTF-16 code units. The count represents mapped ASCII letters, and every mapped letter changes, so unchanged digits and Unicode characters do not inflate it. If clipboard access is denied, the complete read-only output remains available for manual selection.
Transformation, counting, display, and clipboard preparation occur locally in the browser tab. No input, output, or clipboard content is uploaded to Lizely. For a broader comparison of terminal and browser approaches, see ROT13 Command Line vs Online: Picking the Right Path.
Local processing does not make ROT13 confidential. The substitution has no key, so anyone who recognizes ROT13 can reverse it immediately. Use an established cryptographic system when secrecy, authentication, or integrity is required.
Verify the decoded result
A reliable check should cover both the transformation and its self-inverse property. In Python, this assertion compares the twice-transformed value with the original string:
assert codecs.decode(codecs.decode(text, "rot_13"), "rot_13") == text
This works for text with mixed case, punctuation, whitespace, digits, and non-ASCII characters because all unmapped content is preserved. When checking browser output manually, include at least one uppercase letter, one lowercase letter, one unchanged character, and one non-ASCII character. Confirm both the returned text and the changed-letter count, then run the output through ROT13 again if you need to prove that it recovers the source.
If you're weighing options, What Should I Compare Before Using ROT47 Encoder Decoder? covers this in detail.