Replace letters in secret text

I want to change each letter in the text to the next next letter. But this program does not work. Does anyone know why. Thank you in advance. There is also a slight problem with y and z.

import string

letters = string.ascii_lowercase
text=("g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj. ")
for x in range(1,24):
    text.replace(letters[x],letters[x+2])
print(text)
+3
source share
8 answers

Strings are immutable in python.

So text.replacereturns a string, but does not change the original string.

Given that you should not actually use it text.replace, as you will have to change line 24 (or probably 26, see below). Most likely, you can create a translation table to make all changes at once and use it string.translate.

, string.maketrans ( "y" "z"? , , , "", 'b'). translate.

+8

, :

>>> rot = lambda xs: (xs + [xs[0]])[1:]
>>> apply = lambda n,f,x: (n == 0) and x or f(apply(n-1,f,x))
>>> abc = map(chr,range(ord('a'),ord('z')+1))
>>> d = dict(zip(list(abc),apply(2,rot,(list(abc)))))
>>> s = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj. "
>>> reduce(lambda a,b:a+b,map(lambda c: d.get(c,' '),list(s)),"")
'i hope you didnt translate it by hand  thats what computers are for  doing it in by hand is inefficient and that s why this text is so long  using string maketrans   is recommended  now apply on the url  '
+4

, , . , :

text = text.replace(a, b)

, , . , , join .

, .

+3

, , ...

text = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj. "
convert = lambda t: chr(ord('a') + (ord(t) - ord('a')) % 26)
txt_mod = ''.join(convert(chr(ord(t) + 2) if 'a' <= t <= 'z' else t) for t in text)
+2

:

letters = string.ascii_lowercase
uletters = string.ascii_uppercase
text=("g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj. ")
for x in range(0,26):
    text=text.replace(letters[x-2],uletters[x])
print(text.lower())
+2
import string    

s = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj. "    
trans = string.maketrans('abcdefghijklmnopqrstuvwxyz', 'cdefghijklmnopqrstuvwxyzab')
string.translate(s, trans)
+2

. . didn't use string.translate, @mangobug. , .

import string
def puzzle(x):
    y=" "
    for i in x:
        if ord("a")<= ord(i)<=ord("x"):
            n=ord(i)+2
        elif ord(i)==ord("y"):
            n=ord("a")
        elif ord(i)==ord("z"):
            n=ord("b")
        else:
            n=ord(i)
        y=y+chr(n)
    return y

x="g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
y=puzzle(x)
print y
+1

?

, : 1)

for x in range(1,24):

- ?

2)

text.replace(letters[x],letters[x+2])

, x == 23? ((x + 2)% len ()) - .

, , ? , python , , , ?

:

text = text.replace(letters[x],letters[x+2])
0

Source: https://habr.com/ru/post/1726747/


All Articles