Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

no of words in a string in python

def word_count(string):
    # Here we are removing the spaces from start and end,
    # and breaking every word whenever we encounter a space
    # and storing them in a list. The len of the list is the
    # total count of words.
    return(len(string.strip().split(" ")))

string="Python is an interpreted, high-level, general-purpose programming language"
print("'{}'".format(string),"has total words:",count_words(string))
string2=" Hi. My name is Ashwini "
print("'{}'".format(string2),"has total words:",word_count(string2))
Source by www.pythonpool.com #
 
PREVIOUS NEXT
Tagged: #words #string #python
ADD COMMENT
Topic
Name
7+1 =