How to Process Text into Words in Python

Splits a string into words, strips punctuation, and returns a list of uppercase words using a loop.

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

Python code

16 lines
Python 3.9+
def convert_text_processor(text):
    words = text.split()
    processed = []
    
    for word in words:
        clean = word.strip('.,!?;:')
        if len(clean) > 0:
            processed.append(clean.upper())
    
    return processed

if __name__ == "__main__":
    sample_text = "Hello, world! This is a Python exercise for beginners."
    result = convert_text_processor(sample_text)
    print(result)
    print(f"Total words processed: {len(result)}")

Output

stdout
['HELLO', 'WORLD', 'THIS', 'IS', 'A', 'PYTHON', 'EXERCISE', 'FOR', 'BEGINNERS']
Total words processed: 9

How it works

The split() method splits the input string on whitespace into a list of raw word tokens, including punctuation. A for loop iterates over each token, and strip('.,!?;:') removes any leading or trailing punctuation characters. The cleaned word is appended to the processed list in uppercase using .upper(). This pattern is a common first step in text preprocessing.

Common mistakes

  • Forgetting to strip punctuation, leaving unwanted characters in the output
  • Not filtering out empty strings after stripping, which can happen with punctuation-only tokens
  • Using `remove()` instead of `append()` in the loop, which would try to remove items rather than add them

Variations

  1. Use a list comprehension: `[word.strip('.,!?;:').upper() for word in text.split() if len(word.strip('.,!?;:')) > 0]`
  2. Use `re.findall(r'\b\w+\b', text)` to extract words directly with a regex pattern

Real-world use cases

  • Normalizing user input text before passing it to a search or autocomplete feature.
  • Cleaning and uppercasing product names from a CSV export before comparison.
  • Preparing free-text survey responses for frequency analysis in a data pipeline.

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.