How to replace typed letter in python?

1

Hello everyone, I want to develop a script that will replace a letter typed by another one, so it would look like a = e, b = f.

I spent some time trying to create an algorithm for this function but the interpreter only replaced one letter at a time, in case I would like to do this for the whole sentence.

y = []
x = raw_input('reading: ')
print(x)
for i in x:
    if (x) == 'a':
        x = str('e')
        y.append(str('e'[0]))
        if (x) == 'b':
            x = str('f')
            y.append(str('f'[1]))
            if (x) == 'c':
                x = 'g'
                y.append(x[2])
                if (x) == 'd':
                    x = 'h'
                    y.append(x[3])
                    if x == 'e':

above a piece of code. I know there are better ways but I'm still a beginner .. I was hesitant to use the while or for, I ask you to orient me.

    
asked by anonymous 03.05.2017 / 03:26

2 answers

6
  

Remembering that in Python 3, input is used instead of raw_input . As the question uses the second, it was assumed that it refers to Python 2.

The best way is to use the translate method of the string object:

from string import maketrans

table = maketrans('abcd', 'efgh')

x = raw_input("Reading: ")

print(x.translate(table))

See working at Repl.it

The function maketrans constructs the conversion table, indicating in the first parameter the characters that will be replaced and, in the second parameter, the characters that will be inserted respectively.

You can still use replace of the string objects :

x = raw_input("Reading: ")

x = x.replace("a", "e")
x = x.replace("b", "f")

print x

For a abcba input, the output generated is efcfe .

>>> Reading: abcba
efcfe

See working in Repl.it

If there are too many letters, you can use the Marlysson hint in the comments:

letters = {
    "a": "e",
    "b": "f",
    "c": "g"
}

x = raw_input("Reading: ")

for l in letters:
    x = x.replace(l, letters[l])

print x

The output would be:

>>> Reading: abcba
efgfe

See working at Repl.it

You can still make an analog code using list compression:

letters = {
    "a": "e",
    "b": "f"
}

x = raw_input("Reading: ")

x = "".join([letters[l] if l in letters else l for l in x])

print x

See working in Repl.it

    
03.05.2017 / 03:49
1

Resuscitating the topic ... But anyone who still has any doubts on how to do this, I believe a character rotation algorithm ( César Cipher , in some cases, can be easier and more dynamic than the maketrans or a from strings:

import string

rot = 4
alphabet = string.ascii_lowercase
alphabet_length = len(alphabet)

text = raw_input("Digite a frase: ")

result = []
for letter in text:
    if letter.lower() in alphabet:
        unicode_point = ord("a") if letter.islower() else ord("A")
        start = ord(letter) - unicode_point            
        offset = ((start + rot) % len(alphabet)) + unicode_point
        result.append(chr(offset))
    else:
        result.append(letter)

print "".join(result)

Ex:

Digite a frase: abcde
efghi
    
05.04.2018 / 00:21