How to Build a Text Processor with Lists and Loops in Python

A beginner-friendly Python script that analyzes text by counting sentences, words, and word lengths using lists and for loops, then prints the results.

Easy Python 3.9+ Aug 9, 2026 Lists & loops 14 views 0 copies

Python code

29 lines
Python 3.9+
def process_text(text):
    """Simple text processor for beginners using lists and loops."""
    sentences = text.replace('!', '.').replace('?', '.').split('.')
    words = text.split()
    
    word_counts = []
    for sentence in sentences:
        sentence_word_count = len(sentence.split())
        word_counts.append(sentence_word_count)
    
    word_lengths = []
    for word in words:
        word_lengths.append(len(word))
    
    print(f"Original text: {text}")
    print(f"Number of sentences: {len([s for s in sentences if s.strip()])}")
    print(f"Number of words: {len(words)}")
    print(f"Words per sentence: {word_counts}")
    print(f"Word lengths: {word_lengths}")
    
    uppercase_words = []
    for word in words:
        uppercase_words.append(word.upper())
    
    print(f"Uppercase words: {' '.join(uppercase_words)}")

if __name__ == "__main__":
    sample_text = "Hello world! Python is fun. Practice daily."
    process_text(sample_text)

Output

stdout
Original text: Hello world! Python is fun. Practice daily.
Number of sentences: 3
Number of words: 6
Words per sentence: [2, 3, 2]
Word lengths: [5, 5, 6, 3, 8, 5]
Uppercase words: HELLO WORLD! PYTHON IS FUN. PRACTICE DAILY.

How it works

The code splits the text into sentences by replacing ! and ? with periods, then splitting on '.'. It uses a for loop to count words in each sentence and appends the counts to a list. Similarly, it splits the whole text into words and uses another loop to compute each word's length. The results are printed using f-strings, and a final loop converts every word to uppercase and joins them. This approach demonstrates basic list accumulation and iteration patterns.

Common mistakes

  • Using text.split('.') without replacing other punctuation, so sentences ending in ! or ? are missed.
  • Counting empty strings from trailing periods as sentences or words.
  • Forgetting that split() without arguments handles multiple spaces.
  • Confusing the list index of word_counts with the actual sentence index.

Variations

  1. Use a list comprehension like [len(s.split()) for s in sentences] to build word_counts more concisely.
  2. Use re.split(r'[.!?]', text) from the re module to handle all punctuation at once.

Real-world use cases

  • Generating reading level scores by analyzing sentence and word lengths on a blog platform.
  • Building a simple text statistics tool for a content management system to show editors metrics.
  • Creating a word count feature in a note-taking app to help users track their writing length.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Lists & loops

Related tutorials and quizzes for this topic.