Q: replacing letters in a string if they are in a string of letters I have already checked to see if there is a solution for my question, before asking this one. 
I am doing python. I am attempting to write code that takes a input string and for each letter in that string that is a vowel (aeiou), replace that vowel with itself 4 times. Then add an exclamation point (!) at the end. Example:
apple would then look like aaaappleeee!.
I have tried multiple times to make this work with no effect. Wondering if you guys can take a look at my code and see what I have been doing wrong. Hugs and kisses. Christopher
def exclamation (string):
    for i in range(0,len(string)):
        if string[i] in 'aeiou':
            string.replace(string[i],string[i*4])
        print(string + '!')


A: You can use a regular expression for this:
def exclamation(string):
    newString = re.sub(r'([aeiou])', r'\1\1\1\1', string)
    return newString + '!'


A: Strings are immutable so replacing the string returns a new string every time. Instead just build a string as you go along like this:
def exclamation (string):
    buildString = ""
    for i in range(0,len(string)):
        if string[i] in 'aeiou':
            buildString = buildString +string[i]*4
        else:
            buildString = buildString +string[i]
    print buildString+"!"


A: def exclamation(string):
    new_string = ''
    for c in string:
        if c in 'aeiou':
            new_string += c * 4
        else:
            new_string += c
    new_string += '!'
    return new_string


A: replace don't modify the existing string, it return. one more thing never modify list, string or dictionary while iterating over it. using string as variable name is bad practice.
def exclamation(my_string):
    new=''
    for i in range(0,len(my_string)):
        if my_string[i] in 'aeiou':
            new += my_string[i]*4
        else: new += my_string[i]
    print(new + '!')

output:
>>> exclamation('abcdef')
aaaabcdeeeef!

using lambda and map:
>>> def exclamation(my_string):
...     return "".join(map(lambda x:x in 'aeiou' and x*4 or x,my_string))+'!'
... 
>>> exclamation('abcdef!')
'aaaabcdeeeef!'


A: You can iterate over a string, so you don't have to mess with range, and str.replace doesn't modify the existing string. Taken together, you can just do this:
def exclamation(string):
    for c in "aeiou":
        string = string.replace(c, c*4)
    print(string + "!")


A: You can also use list comprehension:
>>> def exclamation(s):
...    return ''.join([x*4 if x in 'aeiou' else x for x in 'apple']) + '!'
... 
>>> exclamation('apple')
'aaaappleeee!'

