Find "one letter that appears twice" in a string

I'm trying to catch if one letter that appears twice in a string using RegEx (or maybe there's some better ways?), for example my string is:

    ugknbfddgicrmopn

The output would be:

    dd

However, I've tried something like:

    re.findall('[a-z]{2}', 'ugknbfddgicrmopn')

but in this case, it returns:

    ['ug', 'kn', 'bf', 'dd', 'gi', 'cr', 'mo', 'pn']   # the except output is `['dd']`

I also have a way to get the expect output:

    >>> l = []
    >>> tmp = None
    >>> for i in 'ugknbfddgicrmopn':
    ...     if tmp != i:
    ...         tmp = i
    ...         continue
    ...     l.append(i*2)
    ...     
    ... 
    >>> l
    ['dd']
    >>>

But that's too complex...

If it's 'abbbcppq', then only catch:

    abbbcppq
     ^^  ^^

So the output is:

    ['bb', 'pp']

Then, if it's 'abbbbcppq', catch bb twice:

    abbbbcppq
     ^^^^ ^^

So the output is:

    ['bb', 'bb', 'pp']

You need use capturing group based regex and define your regex as raw string.

    >>> re.search(r'([a-z])\1', 'ugknbfddgicrmopn').group()
    'dd'
    >>> [i+i for i in re.findall(r'([a-z])\1', 'abbbbcppq')]
    ['bb', 'bb', 'pp']

or

    >>> [i[0] for i in re.findall(r'(([a-z])\2)', 'abbbbcppq')]
    ['bb', 'bb', 'pp']

Note that , re.findall here should return the list of tuples with the characters which are matched by the first group as first element and the second group as second element. For our case chars within first group would be enough so I mentioned i[0].

From: stackoverflow.com/q/34261346