How to replace letter typed in python?

Asked

Viewed 4,682 times

2

hello to everyone I want to develop a script that replaces one typed letter with another, so it would look like a = and , b = f .

I spent some time trying to create an algorithm for this function but the interpreter only replaces a letter at once , 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 beginner .. I was in doubt about using while or for , I ask you to guide me.

  • You can use dictionaries to associate each letter.. It would look : { "a":" and" , "b" : "f" ...} .. Ai then just turn the typed string into a list and for each element get the corresponding in the dictionary.. Transforming again..

  • Your if commands are wrongly chained. Study the Else clause.

2 answers

7

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

The best way is by using the method translate of the object string:

from string import maketrans

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

x = raw_input("Reading: ")

print(x.translate(table))

See working on 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 the method replace of objects string:

x = raw_input("Reading: ")

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

print x

For an entrance abcba, the output generated is efcfe.

>>> Reading: abcba
efcfe

See working on Repl.it

If there are many letters, you can use the tip from Marlysson, 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 exit would be:

>>> Reading: abcba
efgfe

See working on 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 on Repl.it

1

Resurrecting the topic... But anyone who still has any doubts about how to do this, I believe a character rotation algorithm(Cipher of Caesar), in some cases, may easier and more dynamic than the maketrans or a from/to string:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.