close

How to count words in string in python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about How to count words in string in Python. so without wasting time lets learn about of this.

How to count words in string in python

  1. count words in string in python

    to count words in string in python just Use separator. By using separator you can count words in string in python. It is very easy to use. Lets learn about of this by given below example:
    mylist = 'pizza, burger, panipuri, momos, frenky' result = len(mylist.split(',')) print(mylist.split(',')) print("There are " + str(result) + " words.")
    Output :
    ['pizza', ' burger', ' panipuri', ' momos', ' frenky'] There are 5 words.

  2. How to count words in string in python

    to count words in string in python just Use len(). By using len() you can count words in string in python. It is very easy to use. Lets learn about of this by given below example: mylist = 'pizza, burger, panipuri, momos, frenky' result = len(mylist.split()) print("There are " + str(result) + " words.") Output :
    There are 5 words.

  3. python count words

    To count words in string in python just Use len(). By using len() you can count words in string in python. It is very easy to use. Lets learn about of this by given below example: mylist = 'pizza, burger, panipuri, momos, frenky' result = len(mylist.split()) print("There are " + str(result) + " words.") Output :
    There are 5 words.

Method 1: Use separator

By using separator you can count words in string. It is very easy to use. Lets learn about of this by given below example:

mylist = 'pizza, burger, panipuri, momos, frenky'
result = len(mylist.split(','))
print(mylist.split(','))
print("There are " + str(result) + " words.")

Output :

['pizza', ' burger', ' panipuri', ' momos', ' frenky']
There are 5 words.

Method 2: Use len()

By using len() you can count words. It is very easy to use. Lets learn about of this by given below example:

mylist = 'pizza, burger, panipuri, momos, frenky'
result = len(mylist.split())
print("There are " + str(result) + " words.")

Output :

There are 5 words.

Method 3: Use RegEx module

By using RegEx module you can count words. It is very easy to use. Lets learn about of this by given below example:

import re
text = 'pizza, burger, panipuri, momos, frenky'
result = len(re.findall(r'\w+', text))
print("There are " + str(result) + " words.")

Output :

There are 5 words.

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment