答案為3(用正則):;>>> import re>>> s= "abababab">>> len(re.findall(r"(?=aba)", s))3;答案為2,用字串的count方法:;>>> import string>>> s= "abababab">>> s.count("aba")2; ;替換第二個"aba’為‘bab’,用字串的切片方法(可能方法醜陋了點,初學者見諒):;>>> s= "abababab">>> pos = s.find("aba")>>> sNew = s[pos+1:].replace("aba", "bab", 1)>>> if pos == 0:... s[pos] + sNew ... elif pos > 0:... s[:pos] + sNew... "abbabbab"
答案為3(用正則):;>>> import re>>> s= "abababab">>> len(re.findall(r"(?=aba)", s))3;答案為2,用字串的count方法:;>>> import string>>> s= "abababab">>> s.count("aba")2; ;替換第二個"aba’為‘bab’,用字串的切片方法(可能方法醜陋了點,初學者見諒):;>>> s= "abababab">>> pos = s.find("aba")>>> sNew = s[pos+1:].replace("aba", "bab", 1)>>> if pos == 0:... s[pos] + sNew ... elif pos > 0:... s[:pos] + sNew... "abbabbab"