Skip to content

Latest commit

 

History

History
14 lines (10 loc) · 453 Bytes

count-words-in-a-string.md

File metadata and controls

14 lines (10 loc) · 453 Bytes

Count Words In A String

Category: Python

You can use the split() function to count words in a string.

The following method will strip whitespace from the start and end of a string, tokenize a string based on the space character, and return the length of the list which is the number of words.

def count_words(string):
    word_count = (len(string.strip().split(" ")))
    print("Word count", word_count)
    return word_count