Python Program to print words with their length of a string

Here, we will figure out how to print the length of the words from a string in Python? To extricate the words from the string, we will utilize String.split() technique and to get word’s length, we will utilize len() strategy.

Given a string and we need to part the string into words and furthermore print the length of each word in Python.


    str = "Hello World How are you?"

    Hello ( 5 )
    World ( 5 )
    How ( 3 )
    are ( 3 )
    you? ( 4 )

String.split() Method:

To part string into words, we utilize the split() technique, it is an inbuilt strategy which parts the string into set of sub-string (words) by a given delimiter.

split() Method Syntax:



For example, there is string str = “ABC PQR XYZ” and we need to part into words by isolating it utilizing space, at that point space will be delimiter here. To part the string to words, the announcement will be str.split(” “) and afterwards, output will be “ABC” “PQR” “XYZ“.


# Function to split into words
# and print words with its length

def splitString (str):
	# split the string by spaces
	str = str.split (' ')
	# iterate words in string
	for words in str:
		print words," (", len (words), ")"

# Main code 
# declare string and assign value 
str = "Hello World How are you?"

# call the function


    Hello  ( 5 )
    World  ( 5 )
    How  ( 3 )
    are  ( 3 )
    you?  ( 4 )

Leave a Comment

error: Alert: Content is protected!!