Search code examples
pythonfunctiondouble

Write a function or count double letters


I am new to programming and have just learned to basics of strings, slicing and joining. My first assignment is to create a function that takes a set of strings and returns the count of double letters. I am working in python and have been struggling to figure this out.

def twin(words):
    pairs = 0
    for i in range(len(words)-1):
        if words[i] == words[i+1]:
            pairs = pairs +1
        return words

This is what I’ve come up with so far


Solution

  • If we're only taking double letters that are back to back into count, then:

    def twin(sentence):
        counter = 0
        for i in range(len(sentence) - 1):
            if sentence[i] == sentence[i+1]:
                counter += 1
        return counter
    
    
    print(twin('Look book better'))