How to Count Number of Words in Python String?

03-Mar-2023

.

Admin

How to Count Number of Words in Python String?

Hi Dev,

This tutorial will provide example of how to count number of words in python string. We will use python string count number of words. you will learn python program to count number of words in string. this example will help you python count number of char in string. follow bellow step for how to count number of words in string python.

In this examples, i will add myString variable with hello string. Then we will use len() and split() function to count number of words from string in python. So, without further ado, let's see simple examples: You can use these examples with python3 (Python 3) version.

Python String Count Number of Words


main.py

# Declare String Variable

myString = "Hello, This is nicesnippets.com"

# Count Number of Words from String

wordList = myString.split()

numberOfWords = len(wordList)

print(numberOfWords)

Output:

4

#Python