IT & IoT Security | Cloud | It's all about the life itself

Nothing in life is as important as you think it is, while you are thinking about it.

Python – Let’s count a word in a string

Hi, I guess this is not the best practice but it works 🙂

Let’s count “cat” in a given string.

mystery_string = "why cat wy dog cat cacacat cacacat o cat bye cat cat"
uzunluk_my_string = len(mystery_string)
cat_sayisi = 0
for i in range(0,uzunluk_my_string):
    if mystery_string[i] == "c":
        #print(i," ",mystery_string[i])
        if mystery_string[i+1] == "a":
            if mystery_string[i+2] == "t":
                cat_sayisi = cat_sayisi + 1


# The output is 7