where to buy misoprostol online how to buy valtrex
rot13 in Python 3.x | Evan Fosmark

rot13 in Python 3.x

As of Python 3.0, rot13 is no longer built accessible from the str.encode("rot13") call. If needed, here is an implementation I pieced together:

from string import ascii_uppercase, ascii_lowercase
 
def rot13(data):
    """ A simple rot-13 encoder since `str.encode('rot13')` was removed from
        Python as of version 3.0.  It rotates both uppercase and lowercase letters individually.
    """
    total = []
    for char in data:
        if char in ascii_uppercase:
            index = (ascii_uppercase.find(char) + 13) % 26
            total.append(ascii_uppercase[index])
        elif char in ascii_lowercase:
            index = (ascii_lowercase.find(char) + 13) % 26
            total.append(ascii_lowercase[index])
        else:
            total.append(char)
    return "".join(total)

Pretty simple, right? Knowing how modulus (%) works helped greatly in finding the proper index. I hope this helps.

Update: There’s a simpler solution on the comments below. You should probably use it instead.

 

 

5 Comments

  1. James Bennett wrote,

    Simpler and probably more efficient:

    rot13_trans = str.maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm')
     
    def rot13(text):
        return text.translate(rot13_trans)
  2. Zack C wrote,

    @James

    My knee jerk reaction was to want to use string.ascii_uppercase/lowercase, but it doesn’t beat yours in terms of brevity or clarity.

    from string import ascii_uppercase as uc, ascii_lowercase as lc
    rot = uc[13:]+uc[:13] + lc[13:]+lc[:13]
    rot13_trans = str.maketrans(uc+lc, rot)
    def rot13(text):
        return text.translate(rot13_trans)
  3. iDontGiveMyNameToRandomSitesOnTheInternet wrote,

    The simplest way to do this would probably be:

    import codecs
    codecs.encode("text to be rot13()'ed", "rot13")
  4. t|mm| wrote,

    James Bennet’s version is the way I would have done it. So simple!!

  5. Adam Ziaja wrote,

    #!/usr/bin/env python
    # -*- coding: rot13 -*-
    cevag “Uryyb fgnpxbiresybj!”.rapbqr(“rot13″)

Leave a comment

I Want A Tricor Perscription
cheapest buy Neurontin online
Fedex Overnight Nizoral
Buy Lipitor Mail Order
Cheap Methotrexate Onlinebuy Cheap Methotrexate No Perscription
Lexapro On Line No Perscription
buy Lexapro order online
Buying Neurontin
Discount Nizoral USA Nizoral lowest prices
Cheap Lexapro Overnight Saturday Delivery
Levlen Alternative
Levaquin Promotion
Get Methotrexate NOW Methotrexate shipped from Canada
Methotrexate No S Consult
Nolvadex in Spain
Nizoral best price
Buy Tablets Lasix Online
Order Lasix Free Delivery
Purchase Online Without Prescription Methotrexate
Buy Tablets Proscar Online