Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

ROT13 in Python 3

📅 2012-May-14 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ python, rot13 ⬩ 📚 Archive

Obtaining the ROT13 encoding of a string used to be easy in Python 2.x:

s  = "Hello"
os = s.encode( "rot13" )
print( os ) # "Uryyb"

This no longer works in Python 3.x. The solution that works in Python 3.x is to access low-level functions in the codecs module:

import codecs

s   = "Hello"
enc = codecs.getencoder( "rot-13" )
os  = enc( s )[0]

print( os ) # "Uryyb"

Tried with: Python 3.2.3


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧