Hey all, in this amazing tutorial, we are going to WAP (Python) to find the number of occurrences of any word in a string. So be ready with your code editors as we are going to do this program in the best way possible.
You can find the final code of this program at the end of this article. But I would recommend you to be with us this whole journey as we are going to go through each and every step you need to follow to become the best coder in the world!
Overview:
In this program, we are asked to WAP (Python) to find the number of occurrences of any word in a string. In simple words, it means that we need to make a program that checks for the number of occurrences of a particular word in a sentence (both given by the user).
So we would first ask the user to enter both the string and the word. Then we would use basic split() function, for loop and if statements to check if any word of the string matches with what the user has entered.
*As I have already told you the basics of the program, you can try to do this on your own. See if the answer matches!*
Understanding what we need to do:
Here I will tell you step by step what you need to do.
Step 1: Asking the user for the input
This is the most basic part of the program as we are going to just ask the user to input the main string (stored in variable 'string') and the word (stored in variable 'word').
Here's the code for this step:
Step 2: Finding the occurrence of the word in the string
Now this is the interesting part. To compare each and every word of the string entered by the user to the specific word, we need to first break the string into parts.
We can do this using the .split() command. Doing this would help us in directly comparing each word of the string with the specific word. After splitting the string, we need to run a for loop for every word in the new string (stored in the variable 'n_string').
In the for loop, we would also run an if condition that would compare each and every word of the string to the specified word. If the condition holds true, then the value of the variable 'count' (that stores the value of occurrences of the word) is increased by 1.
After the loop ends, we can easily print the final value of the variable 'count' to the user using the print statement. I hope you understood what we did. If you didn't then please go through each and every line of code carefully, it will surely help!
Here's the code for this step:
So we have successfully found out the best way to WAP (Python) to find the number of occurrences of any word in a string!
Here's the final code:
Also read: WAP (Python) to find the area of a triangle whose coordinates of vertices are given
So here ends our journey where we wrote the program to find the number of occurrences of any word in a string! I hope you learnt something new. And if you did then don't forget to share it as it can help someone struggling in programming.
Also, don't forget to subscribe to our newsletter as we do these coding session regularly.
Again, thanks for reading!
No comments:
Post a Comment