with open("text.txt", "r") as file:
s = file.readline()
i=0
k = 0
kmax = 0
while i< (len(s)-1):
if s[i:i+3] == 'XYZ':
k = k + 1
i+=2
if k > kmax :
kmax = k
else:
k = 0
i+=1
print(kmax)
Внешний жесткий диск не исключил "лень" )))