Q: Efficiently find and replace pattern 'n' + (consonant) I'm sure this is simple but I'm a complete beginner. I'm trying to make a tool where I have a string and search for 'n' + [any consonant] and replace it with that consonant doubled - for example 
aaanq -> aaaqq. 
The only thing I can think of is as you see below with a separate case for each possible nC combination.  Is there a better way to do this? 
nC_test = input('Enter word: ')

if 'nq' in nC_test:
    print('nC pattern found ')
    nC_test = nC_test.replace('nq', 'qq')
    print(nC_test)


A: Strings in python are immutable, so if you want to modify them just convert them into a list:
my_list = list(my_string)

Then create an auxiliar function to check if a character is a consonant.
This can be done in many ways, also you might want to check for capital characters, special consonants (Spanish ñ), so feel free to implement this function so it fits your needs:
def is_consonant(character):
    consonants = ['b','c',...,'z']   # complete the list yourself
    if character in consonants:
        return True
    else:
        return False

Then iterate over your string and check for positions where string[i] is 'n' and string[i+1] is consonant.
for i in range(len(my_list)-1): 
    if my_list[i] == 'n' and is_consonant(my_list[i+1]):
        print('nC pattern found ')
        my_list[i] = my_list[i+1]  

Note that we don't iterate until the last element but until the penultimate one, so we can safely do my_list[i+1].
If at the ens you need a string, just convert the list back to a string:
my_string = ''.join(my_list)

NOTE: Take in account that overlapping cases might give undesired results if you have two consecutive n followed by a consonant:
annq --> annq --> anqq

Here the first n is followed by a consonant (n), then the first n is replaced by the second one (no effective change). Then the next loop sees "nq" and changes it to "qq" but the resulting string contains an "nq" as result.
I don't know if you considered this case, just take it in account. So if your result is supposed to not contain any n followed by consonant you will have to take in account this case.
