Python Program to Count Words in a String Example

By Hardik Savani October 30, 2023 Category : Python

Hello Guys,

If you need to see an example of python count words in string. you will learn how to count words in python string. We will use python program to count words in a string. I would like to share with you python code to count number of words in a string. Here, Create a basic example of python string count number of words .

In this example we will use len() and split() function to count number of words in python string. you can see the simple code of how to count number of words in string in python.

Let's see the python script example:

Example:

main.py

string = "Hello world! This is a string."
  
words = len(string.split())
  
print(words)

Output:

6

I hope it can help you...

Tags :
Shares