first pair of the same symbols

Question:

Find the first (consecutive) pair of the same symbols using for-loop. It doesn’t matter if there are more than 2 same consecutive symbols.

Example:

"abBcccc" returns "cc"

What I have so far:

result = []
    for c in range(len(text) - 1):
        if text[c] == text[c + 1]:
            result.append(text[c])
    print("".join(result))
Asked By: fallguy

||

Answers:

Using iterators with index seems to be the only way here:

string = "abBcccc"

for index in range(0, len(string)-1):
    if string[index] == string[index+1]:
        print(string[index:index+2])
        break

It’s needed to break to only get the first one.

Answered By: Enrico Agrusti
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.