How to count Words in String in Python

Hi guys. How are you all? I hope you all fine. Today in this tutorial we will learn about How to count Words in String in Python. By using some methods we can count the number of the words in a string in python. So here we are going to learn this methods. Which are very easy and simple. So lets learn about of this by with the examples. I hope you guys like it.

How to count Words in String in Python

  1. Count Words in String in Python

    to count Words in String in Python just Use split() and len(). By using split() and len() we can count the words are present in the struing in pytghon. First of all we will make our string in which we count the words than we will use split() and len() functions with it. And will print it. Lets learn this by given below examples. mytext = 'Lets learn python from ittutorialpoint' var1 = len(mytext.split()) print("There are " + str(var1) + " words.") Output : There are 5 words.

  2. How to count Words in String in Python

    to count Words in String in Python just Use RegEx. We can use RegEx function to count the word in the string> Here first of all e import re and then use re.findall() function to count the word in string in python. So lets learn about of this method by given below example. I hope that will help you. import re mytext = 'Lets learn python from ittutorialpoint' var1 = len(re.findall(r'\w+', mytext)) print("There are " + str(var1) + " words.") Output : There are 5 words.

Method 1: Use split() and len()

By using split() and len() we can count the words are present in the struing in pytghon. First of all we will make our string in which we count the words than we will use split() and len() functions with it. And will print it. Lets learn this by given below examples.

mytext = 'Lets learn python from ittutorialpoint'
var1 = len(mytext.split())
print("There are " + str(var1) + " words.")

Output :

There are 5 words.

Method 2: Use RegEx

We can use RegEx function to count the word in the string> Here first of all e import re and then use re.findall() function to count the word in string in python. So lets learn about of this method by given below example. I hope that will help you.

import re
mytext = 'Lets learn python from ittutorialpoint' 
var1 = len(re.findall(r'\w+', mytext))
print("There are " + str(var1) + " words.")

Output :

There are 5 words.

Method 3: Use sum(),strip() and split()

By using sum(), strip() and split() function you can count the word present in the string in python. First we will import string and then make our string in which we want to count the words. Then we will use sun, string and split functions and we can count the words by it. You can see this in given below output. So lets learn this by given below example. I hope you guys like it. Its very easy and simple method. Lets learn it.

import string
mytext = 'Lets learn python from ittutorialpoint' 
var1 = sum([i.strip(string.punctuation).isalpha() for i in mytext.split()])
print("There are " + str(var1) + " words.")

Output :

There are 5 words.

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment