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.
We have following python script which is named
sentence_word_count.py . 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): count=len(s.split()) return count print("Sentence:") print(sys.argv) print("Total Word Count:") print(countwords(sys.argv))
Run Python Script
We will use
sentence_word_count.py script in order to count given sentence word count. In this case sentence is
This is a sample sentence.
$ python sentence_word_count.py "This is a sample sentence" Sentence: This is a sample sentence Total Word Count: 5