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.
Python code
29 linesdef 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
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
- Use a list comprehension like [len(s.split()) for s in sentences] to build word_counts more concisely.
- 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
More from Lists & loops
- Check if List is Sorted Ascending in Python easy
- Compare Two Lists in Python: Common, Only in First, Only in Second easy
- Convert a List of Integers to a Comma-Separated String in Python easy
- Enumerate a Python List with a Custom Start Index easy
- Extract Data by Type from a List in Python: Numbers and Strings easy
- Find All Occurrences of an Item in a Python List easy
Keep learning
Related tutorials and quizzes for this topic.