определить сколько в строке одинаковых соседних символов in python

To determine how many consecutive same characters are there in a string, you can iterate over the string and keep track of the previous character. If the current character is the same as the previous one, increase a counter. If the current character is different from the previous one, reset the counter. Here's an example code:

main.py
s = "aaabbbbccc"
count = 0
prev_char = None

for char in s:
    if prev_char == char:
        count += 1
    else:
        if count > 0:
            print(prev_char, count)
        prev_char = char
        count = 1

if count > 0:
    print(prev_char, count)
259 chars
16 lines

This will output:

main.py
a 3
b 4
c 3
12 chars
4 lines

In this example, we assume that the string only contains letters. If your string might contain non-letter characters, you may want to add a check to only count consecutive same alphanumeric characters.

gistlibby LogSnag