How To Count Words In A Sentence In Python? – POFTUT

How To Count Words In A Sentence In Python?

I want to send my text to the python application as argument. The application will return the count of words in the text. Here is the basic but useful sample python application.

Python Script

We have following python script which is named . It gets sentence as argument and split it according to spaces. Uses len() function in order to count elements in the given list.

import sys 
def countwords(s): 
    return count 
print("Total Word Count:") 
Python Script 
Python Script

Run Python Script

We will use script in order to count given sentence word count. In this case sentence is This is a sample sentence.

$ python "This is a sample sentence" 
This is a sample sentence 
Total Word Count: 

LEARN MORE  What Is A Function and How To Create Function In PHP, Python, JavaScript, C/C++, C#, Bash, Java , PowerShell Programming Languages?

Leave a Comment