Master Python Coding Logic! 🧠Step-by-Step Logic Building Program

Learn to count character occurrences in a string using Python. The tutorial builds a for-loop with an if-statement that checks each character and increments a counter, then prints the total. The example counts three G's in "Coding Didi Programming".
Chapters
Now let us work on counting the number of occurrence of any particular character in a given word. Yes, we are going to work on one single character and we are going to find it in string and see how many times that particular character occur. So for this, I am going to create one variable word in which I am going to store a string which can be anything. It can be a sentence, it can be a word, it can be multiple words. I am using two words. Coding, DD programming is the word which I have used. Now I am trying to find out the character. Which character you have to find out, you have to specify it. You can even take input for both of the things. I am using G character and then creating a count variable in which for i in word, it means I am going to iterate over the word variable and say that if i is equal to equal to the character, it means if this G is the exact character, then it means I am going to update the count variable. Otherwise I will say that do not do anything, go to the next iteration. And outside of the for loop, I am going to say print count. Simple. Now let us run this, then I will take you through the entire thing. So 3 times we are getting G. So in Coding Didi Programming, we have in total 3 times G. Now let us go through the code. Now look at it very carefully what we are doing. So we have a string which is in the variable word, Coding Didi Programming. Correct. So this is input, this is a value, it can be taken from the input function as well. Then g, g is a character that we are going to find out. Again, this is an input, right? After doing this, I am creating one variable called count with the 0. So assume it like a container which is totally empty. I am storing 0, it means the container is totally empty, it doesn't have any value for now. Now I am using for loop for iteration where I will go one by one to each character. So let's say first character is C. So is C equal to G? No, it is not. So if it is not, it means if the condition goes false, then we do not have to do anything. We have to go to the next iteration. Next iteration means next character in the word. So next character in the word is O. So O is G? No, again it is gonna be false. So it means next iteration, then D, then I, N. The condition is gonna be false every time, but once it will become true It means now it's time to go to the container which we have created, count, and we are going to update it with 1. And this is how we are going to each character and going to scan through it and see wherever we are able to see G. So we have the G again, so we will do 1 1. Then R, A, double M is also not G. So whichever character is not the character which we are finding out, so we will not do anything. But once we will find it, we will keep on updating the count variable with 1. Therefore, we are getting 3 as in the final value.










