ItSolutionStuff.com

Python Program to Count Words in a String Example

By Hardik Savani • October 30, 2023
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: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get Only Negative Values in Python List?

Read Now →

Python Count Number of Specific Characters in String Example

Read Now →

Python Count Number of Words in String Example

Read Now →

Python Remove First Character from String Example

Read Now →

Python Dictionary Check if Value Exists Example

Read Now →

Python Dictionary Remove Element by Value Example

Read Now →

Python Dictionary Delete Item by Key Example

Read Now →

How to Add Element in Dictionary Python?

Read Now →

Python Read CSV File Without Header Example

Read Now →

How to Add Header in CSV File using Python?

Read Now →

How to Check If a List is Empty or Not in Python?

Read Now →

Python Create Text File If Not Exists Example

Read Now →

Python Create Text File in Specific Directory Example

Read Now →