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.
Python code
16 linesdef 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
['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
- Use a list comprehension: `[word.strip('.,!?;:').upper() for word in text.split() if len(word.strip('.,!?;:')) > 0]`
- 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
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.