计算字符串的最后一个字符-不读取最后一个字符

时间:2020-06-08 20:24:39

标签: python python-3.x

while(i <= len(word_noSpaces)- 1):

    count = 1

    while word_noSpaces[i] == word_noSpaces[i+1]: 

        i = i + 1
        count +=1
        if  i + 1 == len(word_noSpaces):
            break
    print(str(word_noSpaces[i]) + " " + str(count), end = "\n")
    i+=1
print()    

1 个答案:

答案 0 :(得分:0)

请考虑以下内容:

import re

word_noSpaces="xyzccfjjkkkvbmbdfj"

for el in re.finditer(r"(.)\1+", word_noSpaces):
    el=el[0]
    print(el)
    print(len(el))

它将打印所有重复的字符及其长度。

有关re软件包的更多信息:

https://docs.python.org/3/library/re.html