Skip to main content
added 93 characters in body
Source Link

Python strings have a translate method that applies a substitution cipher. There is also a function str.maketrans (string.maketrans in Python 2) that helps with building the translation table:

>>> from string import ascii_lowercase as alphabet
>>> shift = 13
>>> cipher = str.maketrans(alphabet, alphabet[shift:] + alphabet[:shift])
>>> print("caeser salad is tasty".translate(cipher))
pnrfre fnynq vf gnfgl

of coarse in python2(with a shift of 13) one can simply do "My Message".encode("rot13")

Python strings have a translate method that applies a substitution cipher. There is also a function str.maketrans (string.maketrans in Python 2) that helps with building the translation table:

>>> from string import ascii_lowercase as alphabet
>>> shift = 13
>>> cipher = str.maketrans(alphabet, alphabet[shift:] + alphabet[:shift])
>>> print("caeser salad is tasty".translate(cipher))
pnrfre fnynq vf gnfgl

Python strings have a translate method that applies a substitution cipher. There is also a function str.maketrans (string.maketrans in Python 2) that helps with building the translation table:

>>> from string import ascii_lowercase as alphabet
>>> shift = 13
>>> cipher = str.maketrans(alphabet, alphabet[shift:] + alphabet[:shift])
>>> print("caeser salad is tasty".translate(cipher))
pnrfre fnynq vf gnfgl

of coarse in python2(with a shift of 13) one can simply do "My Message".encode("rot13")

Notice removed Needs detailed answers by Jamal
Brief explanation of the code; port to Python 3; add links; add output
Source Link
Gareth Rees
  • 49.7k
  • 3
  • 129
  • 210

Python strings have a translate method that applies a substitution cipher. There is also a function str.maketrans (string.maketrans in Python 2) that helps with building the translation table:

import>>> from string import ascii_lowercase as alphabet
shift_amt>>> shift = 13
alphabet>>> =cipher string.ascii_lowercase
tab= =stringstr.maketrans(alphabet,alphabet[shift_amt alphabet[shift:] + alphabet[:shift_amt]shift])
print>>> "ceaserprint("caeser salad is tasty".translate(tabcipher))
pnrfre fnynq vf gnfgl
import string
shift_amt = 13
alphabet = string.ascii_lowercase
tab =string.maketrans(alphabet,alphabet[shift_amt:] + alphabet[:shift_amt])
print "ceaser salad is tasty".translate(tab)

Python strings have a translate method that applies a substitution cipher. There is also a function str.maketrans (string.maketrans in Python 2) that helps with building the translation table:

>>> from string import ascii_lowercase as alphabet
>>> shift = 13
>>> cipher = str.maketrans(alphabet, alphabet[shift:] + alphabet[:shift])
>>> print("caeser salad is tasty".translate(cipher))
pnrfre fnynq vf gnfgl
added 1 characters in body
Source Link
import string
shift_amt = 13
alphabet = string.ascii_lowercase
tab =string.maketrans(alphabet,alphabet[-shift_amtalphabet[shift_amt:] + alphabet[:shift_amt])
print "ceaser salad is tasty".translate(tab)
import string
shift_amt = 13
alphabet = string.ascii_lowercase
tab =string.maketrans(alphabet,alphabet[-shift_amt:] + alphabet[:shift_amt])
print "ceaser salad is tasty".translate(tab)
import string
shift_amt = 13
alphabet = string.ascii_lowercase
tab =string.maketrans(alphabet,alphabet[shift_amt:] + alphabet[:shift_amt])
print "ceaser salad is tasty".translate(tab)
Notice added Needs detailed answers by Adam
Post Migrated Here from stackoverflow.com (revisions)
Source Link
Loading