0

I need to replace specific letters with other specific letters, so here's an example of a desired output and input:

Input: QWe RtYy
Output: ABc DeFf

I tried using string.replace but I realized that it would be very long for each uppercase and lowercase letter and what I tried didn't work properly, because only the last letter would change.

Extract of my code:

string = input("Enter text.\n")

NewString = string.replace("q", "a")
NewString = string.replace("Q", "A")
NewString = string.replace("w", "b")
NewString = string.replace("W", "B")

print(NewString)

But when I write "q Q w W" I get "q Q w B" instead of "a A b B".

Chris
  • 26,361
  • 5
  • 21
  • 42
Mochi
  • 13
  • 4

3 Answers3

4

You may wish to use str.translate to effect this change, as shown below in a simple example.

>>> d = str.maketrans({'q': 'a', 'Q': 'A'})
>>> "qQ".translate(d)
'aA'

You could also use the re module to accomplish this with regular expressions. This approach lets us replace multicharacter substrings.

>>> import re
>>> d = {'He': 'l', 'l': 'o'}
>>> s = 'Hello'
>>> r = re.compile(rf"{'|'.join(d.keys())}")
>>> r.sub(lambda m: d[m[0]], s)
'looo'

If we tried to accomplish this with repeated calls to str.replace as you have almost gotten right*, we might see unintended side-effects as the replace will be taking place on the entire transformed string, including the parts that have already been replaced.

>>> s = 'Hello'
>>> s = s.replace('He', 'l')
>>> s = s.replace('l', 'o')
>>> s
'oooo'

* You simply needed to reassign the results of the string replacement back to the same string variable so that all of the replacements take effect.

Chris
  • 26,361
  • 5
  • 21
  • 42
1

You only see the last change because NewString is being overwritten each time - but using the original string. You can instead change each NewString with .replace like this:

string = input("Enter text.\n")

NewString = string.replace("q", "a")
NewString = NewString.replace("Q", "A")
NewString = NewString.replace("w", "b")
NewString = NewString.replace("W", "B")

print(NewString)

Also check out this answer for some efficient ways to replace multiple substrings instead of doing it like this.

Tom Wagg
  • 189
  • 5
0

What you are doing with the case sensitivity is just fine. You just have to assign the NewString a different way--- by doing NewString = string.replace("some", "thing"), you are changing the value of NewString, but from the original text (i.e. your input). So, the only thing that will change is the last character. Instead, you should redefine from your new variable:

string = input("Enter text.\n")

NewString = string.replace("q", "a")
NewString = NewString.replace("Q", "A")
NewString = NewString.replace("w", "b")
NewString = NewString.replace("W", "B")

For versatility, you can use str.split() to split items by whitespace.

string = input("Enter text.\n")

NewString = string.replace(string.split()[0], "a")
NewString = NewString.replace(string.split()[1], "A")
NewString = NewString.replace(string.split()[2], "b")
NewString = NewString.replace(string.split()[3], "B")

Or, in one line (excluding input):

string = input("Enter text.\n")

NewString = string.replace(string.split()[0], "a").replace(string.split()[1], "A").replace(string.split()[2], "b").replace(string.split()[3], "B")

Hope this helps.