Word CounterImplement Python program to find the number of words in a string Constraints:NAExample:Sample Input:Python Programming LanguageSample Output:3
Question
Word Counter
Implement Python program to find the number of words in a string
Constraints: NA
Example:
Sample Input: Python Programming Language
Sample Output: 3
Solution
Sure, here is a step-by-step guide on how to implement a Python program to find the number of words in a string:
Step 1: Define the string First, you need to define the string that you want to count the words in. For example:
text = "Python Programming Language"
Step 2: Use the split() function
Python's built-in split()
function can be used to split a string into a list where each word is a list item. By default, split()
divides a string into words using space as the separator.
words = text.split()
Step 3: Count the words
You can then use the len()
function to count the number of items in the list, which corresponds to the number of words in the original string.
num_words = len(words)
Step 4: Print the result Finally, you can print the result:
print(num_words)
So, the complete program would look like this:
text = "Python Programming Language"
words = text.split()
num_words = len(words)
print(num_words)
When you run this program, it will print 3
, which is the number of words in the string "Python Programming Language".
Similar Questions
Word CounterImplement Python program to find the number of words in a string Constraints:NAExample:Sample Input:Python Programming LanguageSample Output:3
Write a program to count number of words in a given string whose length is greater than 3.For example:Input Resultpython is easier than C3
Write a Python program to count the number of occurrences of a specific character in a string
Write a python program to count the number of palindrome strings that occurred in the given string.
Write a program to accept a string as input and print the number of consonants in the string.Input:StringOutput:IntegerConstraints1<=length of string <=1000
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.