liveBook · Manning

Average_word_length

As explained in Learn AI-Assisted Python Programming, Second Edition

A book-specific Python function that computes the average number of letters per word while excluding surrounding punctuation and words containing no letters.

average_word_length computes the average number of letters per word. It supports authorship identification by turning word length into one feature of an author's writing style. section 11.3.1

The function processes text in a small sequence of steps:

  • Split the text into candidate words. section 11.5.2
  • Pass each candidate through clean_word so surrounding punctuation is removed. section 11.5.2
  • Ignore empty results, including candidates containing no letters. section 11.5.2
  • Add the remaining word lengths, count those words, and return total letters divided by word count. section 11.5.2

Worked example

For “A pearl! Pearl! Lustrous pearl! Rare. What a nice find.”, punctuation is not counted. The text contains 41 letters and 10 words, so average_word_length returns 41 / 10 = 4.1. section 11.3.1 section 11.5.2

Listing 11.3 Average word length
def average_word_length(text):
    '''
    text is a string of text.

    Return the average word length of the words in text.
    Do not count empty words as words.
    Do not include surrounding punctuation.
    
    >>> average_word_length('A pearl! Pearl! Lustrous pearl! \
Rare. What a nice find.')
    4.1
    '''
    words = text.split()        #1
    total = 0                      #2
    count = 0                #3
    for word in words:        #4
        word = clean_word(word)       #5
        if word != '':              #6
            total += len(word)     #7
            count += 1            #8
    return total / count        #9

The listing shows the function's inputs, counters, cleaning step, empty-word check, accumulation, and final division. section 11.5.2

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Explains the calculation, cleaning rules, counters, and doctest implementation, but does not develop the wider signature comparison.
Examples
Demonstrates punctuation handling with a sample passage containing repeated words, short words, and sentence punctuation.
What it assumes
Assumes basic Python strings, loops, conditionals, and functions while teaching punctuation exclusion and empty-word filtering.
What you can do after
Build it from scratch as a tested Python function that calculates average word length while applying explicit filtering rules.
How it connects
Connects average_word_length to clean_word, authorship identification, writing-style features, and signatures.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.