Calculate average word length of a sentence in Python

When working with strings in Python, sometimes you may need to know the average word length in a sentence. The average word length of a sentence is equal to the sum of the lengths of all characters and the sum of all words.

This article will introduce Python to calculate the average word length in a sentence.


Calculate average word length in a sentence using split(), sum() and len() in Python

It is impossible to count every word in a sentence and find the average word length. But you can split it into a list and apply the function.

The method in Python split()splits a string into a list, where each word is a list item.

After splitting the string into a list, use len()the function to get the average word length. len()The function provides the number of items in the list.

The following example calculates the average word length in a given sentence.

sentence = "Welcome to DelftStack tutorials"
words = sentence.split()
avg = sum(len(word) for word in words) / len(words)
print("Average word length is", avg)

output:

Average word length is 7.0

sum()function to get the sum of the lengths of all characters.


Calculate average word length in a sentence using split(), sum(), map() and len() in Python

The method is the same as above, but here we use the map() function to get the length of the character. map()Function allows you to apply a specified function to all items of an iterable.

As you can see, it replaces the for loop in the first method.

sentence = "Welcome to DelftStack tutorials"
words = sentence.split()
average = sum(map(len, words)) / len(words)
print("Average word length is", average)

output:

Average word length is 7.0

Calculate average word length in a sentence using split(), len() and join() in Python

join()Method concatenates all items in an iterable (list, tuple, string) into a single string. In this method, we use split(), len(), and join() to calculate the average word length in a sentence.

sentence = "Welcome to DelftStack tutorials"
words=sentence.split()
single=''.join(words)
average=len(single)/len(words)
print("Average word length is", average)

Here, we split the sentence into a list and save it in a variable words. Next, we use join()the method to concatenate the items in the list with an empty string.

The result is WelcometoDelftStacktutorials, which is stored in a variable single. We then calculate the average word length by dividing the individual length by the word length.

output:

Average word length is 7.0

Calculate average word length in a sentence using len() and replace() in Python

The replace() method in Python replaces the specified text/characters with new text/characters.

In this case we replace spaces with empty strings. We use the len() function to count the number of characters in a sentence.

Next, we divide the result by the number of items in the list object to get the average word length in the sentence.

sentence = "Welcome to DelftStack tutorials"
average = len(sentence.replace(' ','')) / len(sentence.split())
print("Average word length is", average)

output:

Average word length is 7.0

Now you should know how to split()calculate the average word length of a sentence using the command. The logic behind all methods is very similar.

You also learned to use len()and replace()to determine the average word length. We hope you enjoyed learning about this simple Python program.

Guess you like

Origin blog.csdn.net/fengqianlang/article/details/132136631