4

I am trying to write a function that would let me shift the individual letters in a string. The string comes from the input.

I'm looking at vowels so "a, e, i, o, u"

I would like the individual letter to be shifted one to the right. i.e the world "me" would become "mi" as i is the next letter after e.

this is what I have so far:

import random
vowels = ("a", "e", "i", "o", "u")

message = input("Enter a string")

new_message = ""

for letter in message:
    if letter not in vowels:
        new_message += letter
    else:
        new_message += random.choice(vowels)
        
print(new_message)

However, this randomizes the changing of the individual vowels, what would be the best way to make it shift to the next letter?

3
  • 1
    you want to use the next letter right, then why are you using random ? Jul 24, 2020 at 13:26
  • 5
    create a dict {"a":"e", "e": "i",...} instead of tuple
    – sushanth
    Jul 24, 2020 at 13:26
  • 3
    See also stackoverflow.com/questions/8886947/… which has several interesting variations, some of which will easily accommodate your use case.
    – tripleee
    Jul 27, 2020 at 9:43

5 Answers 5

6

If you want to translate the whole string, then I would look at str.translate.

Here is a simple example working with your vowels:

>>> v = "aeiou"
>>> vDict = {v[i]: v[(i+1)%len(v)] for i in range(len(v))}
>>> vDict
{'a': 'e', 'e': 'i', 'i': 'o', 'o': 'u', 'u': 'a'}
>>> "This is a test-string. hope it works!".translate(str.maketrans(vDict))
'Thos os e tist-strong. hupi ot wurks!'
1
  • I didn't know you could give a dictionary to str.maketrans. Nice! Jul 25, 2020 at 0:39
2

That was true. Using dictionary should fix it

import random
vowels = {"a":"e", "e":"i", "i":"o", "o":"u", "u":"a"}

message = input("Enter a string : ")

new_message = ""

for letter in message:
    if letter not in vowels:
        new_message += letter
    else:
        new_message += vowels[letter]
        
print("Converted text : " + new_message)

output :

Enter a string : hello sir
Converted text : hillu sor
1
  • 2
    An alternative: The complete for loop could be replaced by new_message = ''.join(vowels.get(c, c) for c in message)
    – Matthias
    Jul 24, 2020 at 15:41
1
vowels = ["a", "e", "i", "o", "u"]

mapper = dict(zip(vowels, vowels[1:] + [vowels[0]])
message = input("Enter a string")

new_message = ""

for letter in message:
    new_message += mapper.get(letter, letter)
        
print(new_message)

However, i would recommend using re library

import re

new_message = re.sub("\S", lambda x: mapper.get(x.group(0), x.group(0)))

Note, in both methods, the character is replaced by itself if no corresponding mapper exists, else with the mapper item

0

You can change your method to:

for letter in message:
    if letter not in vowels:
        new_message += letter
    else:
        i = vowels.index(letter)
        i+=1
        new_message += vowels[i]

This way, the function would find the index of the vowel in the vowels array and update it by one to add the next vowel in the new message

1
  • Doesnt account for replacing "u"
    – vestronge
    Jul 24, 2020 at 13:36
0

Maybe your else could look something like this:

next_vowel_index = vowels.index(letter) + 1
if next_vowel_index >= len(vowels):
    next_vowel_index = 0
new_message += vowels[next_vowel_index]

This code takes the index of the current vowel and increments it by one, unless it is the last vowel in the tuple, in which case it sets the index to zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.