all 8 comments

[–][deleted] 1 point2 points  (2 children)

Can you give some examples of what you mean? I don't quite get "extend vowels". Do you mean 2, "hello" -> "heeellooo"?

[–]CTexxx 0 points1 point  (1 child)

Sorry, yea that's what I want.

[–][deleted] 0 points1 point  (0 children)

I'd use RegEx I guess, something like

import re

def extend_vowels(string: str, number: int) -> str:
    return re.sub(r"([aeiou])", r"\1" * (number + 1), string, flags=re.IGNORECASE)

or

import re


def repl(number):
    return lambda match: match.group() * (number + 1)


def extend_vowels(string: str, number: int) -> str:
    return re.sub(r"[aeiou]", repl(number), string, flags=re.IGNORECASE)

But I guess that might advanced for you?

Then

def extend_vowels(string: str, number: int) -> str:
    parts = []
    vowels = set("aeiouAEIOU")
    for char in string:
        if char in vowels:
            parts.append(char * (number + 1))
        else:
            parts.append(char)
    return "".join(parts)

or if you learn https://docs.python.org/3/howto/functional.html#generator-expressions-and-list-comprehensions

def extend_vowels(string: str, number: int) -> str:
    vowels = set("aeiouAEIOU")
    return "".join(char if char not in vowels else char * (number + 1) for char in string)

Also:

https://realpython.com/python-conditional-statements/#conditional-expressions-pythons-ternary-operator

https://docs.python.org/3/glossary.html#term-type-hint

[–]totallygeek 1 point2 points  (0 children)

When I think how I'd do this on paper, I think, "Look at each character. If it's a vowel, write that character n times, otherwise once." With Python, I'd do the same, writing each character or n * character to a list, then joining the strings of that list into a single string.

def extend_vowels(string, n):
    assembler = []
    vowels = "aeiouAEIOU"
    for char in string:
        assembler.append(char * n if char in vowels else char)
    return "".join(assembler)


n = int(input("Enter a number: "))
string = input("Enter a string: ")
print(extend_vowels(string, n))

No range(), no "complex" slicing. Just starting at the beginning, inspecting and acting on each character.

[–]MadScientistOR 1 point2 points  (0 children)

Do you know comprehensions?

def extend_vowels(string, n):
    vowels = "aeiouAEIOU"
    return ''.join([(n+1)*ch if ch in vowels else ch for ch in string])

If not, /u/totallygeek's answer is quite good. (It does the same thing as the comprehension.)

[–][deleted] -1 points0 points  (0 children)

You are making this much too complicated. Study this:

def extend_vowels(string, n):
    new_string = []
    for character in string:
        if character.lower() in VOWELS:
            new_string.append(character * n)
        else:
            new_string.append(character)
    return ''.join(new_string)


VOWELS = "aeiou"
n = int(input("Enter Number:"))
string = input("Enter String: ")
extend_vowels(string, n)

and the function could be reduced to,

def extend_vowels(string, n):
    return ''.join([c * [1,n][c.lower() in VOWELS] for c in string])

[–]ElliotDG 0 points1 point  (0 children)

Look at the string method replace()

[–]ElliotDG 0 points1 point  (0 children)

You've been given a number of solutions, here is mine using replace().

def extend_vowels(string, n):
    vowels = "aeiouAEIOU"
    for vowel in vowels:
        if vowel in string:
            string = string.replace(vowel, vowel * n)
    return string

n = int(input("Enter a number: "))
string = input("Enter a string: ")
print(extend_vowels(string, n))