string – Short rot13 function – Python

string – Short rot13 function – Python

Its very simple:

>>> import codecs
>>> codecs.encode(foobar, rot_13)
sbbone

maketrans()/translate() solutions…

Python 2.x

import string
rot13 = string.maketrans( 
    ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz, 
    NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm)
string.translate(Hello World!, rot13)
# Uryyb Jbeyq!

Python 3.x

rot13 = str.maketrans(
    ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz,
    NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm)
Hello World!.translate(rot13)
# Uryyb Jbeyq!

string – Short rot13 function – Python

This works on Python 2 (but not Python 3):

>>> foobar.encode(rot13)
sbbone

Your email address will not be published. Required fields are marked *