Nairda123
Nairda123

Reputation: 313

How to shift vowels in input string with Python

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?

Upvotes: 4

Views: 1905

Answers (5)

Martijn Bots
Martijn Bots

Reputation: 366

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.

Upvotes: 0

Philip Purwoko
Philip Purwoko

Reputation: 447

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

Upvotes: 2

Hampus Larsson
Hampus Larsson

Reputation: 3100

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!'

Upvotes: 6

vestronge
vestronge

Reputation: 962

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

Upvotes: 1

Kanza Sheikh
Kanza Sheikh

Reputation: 65

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

Upvotes: 0

Related Questions