Find Data From a String in Python: Stats, Clean, Keywords
Three helper functions for beginners: compute character/word/sentence stats, normalize whitespace and case, and extract unique sorted keywords from a string.
Python code
28 linesdef get_text_stats(text):
"""Return basic statistics about a string."""
words = text.split()
sentences = text.replace('!', '.').replace('?', '.').split('.')
sentences = [s for s in sentences if s.strip()]
return {
'characters': len(text),
'words': len(words),
'sentences': len(sentences),
'average_word_length': round(sum(len(w) for w in words) / len(words), 2) if words else 0,
}
def clean_text(text):
"""Remove extra whitespace and normalize case."""
return ' '.join(text.split()).lower()
def extract_keywords(text, min_length=4):
"""Return unique words longer than min_length, sorted alphabetically."""
words = text.lower().split()
keywords = {word.strip('.,!?;:') for word in words if len(word.strip('.,!?;:')) >= min_length}
return sorted(keywords)
if __name__ == "__main__":
sample = " Python is fun! It's powerful and easy to learn. Python helps beginners. "
print("Original:", repr(sample))
print("Stats:", get_text_stats(sample))
print("Cleaned:", clean_text(sample))
print("Keywords:", extract_keywords(sample))
Output
Original: ' Python is fun! It's powerful and easy to learn. Python helps beginners. '
Stats: {'characters': 66, 'words': 11, 'sentences': 3, 'average_word_length': 4.36}
Cleaned: 'python is fun! it's powerful and easy to learn. python helps beginners.'
Keywords: ['beginner', 'easy', 'learn', 'powerful', 'python']
How it works
The get_text_stats function splits on whitespace for words and normalizes punctuation to periods for sentence counting, filtering out empty entries. clean_text uses split() then join() to collapse all whitespace and lowercases the result for a consistent form. extract_keywords lowercases, strips punctuation from each word, and keeps unique words meeting a minimum length, returning them sorted. Each function returns a plain Python object (dict, str, list) so beginners can see exactly what is produced and adapt it. The example block at the bottom ties all three together with a single sample string.
Common mistakes
- Counting sentences by splitting on '.' only, missing '!' and '?' endings
- Forgetting to filter empty strings after splitting on punctuation
- Using `len(text.split())` without handling zero-length input (division by zero)
- Not stripping punctuation before treating words as keywords
Variations
- Use a regex `re.findall(r'\b\w+\b', text)` for more robust word extraction
- Add a `stopwords` set to exclude common words like 'the' or 'and' from keywords
Real-world use cases
- Quick text summarization pre-processing before feeding data to a language model.
- Content management systems highlighting topic tags from article titles or body text.
- Log analysis scripts that normalize free-text fields before deduplication or search.
Sponsored
More from Strings & text
- Automatically Detect Weak Passwords from Large Password Lists in Python easy
- Build CSV row from Python list with proper quoting easy
- Build a Secure Password Strength Checker in Python easy
- Convert Natural Language Dates to Datetime in Python medium
- Count Characters, Words, and Lines in Python Text easy
- Extract Data from Strings in Python: Beginner's Guide easy
Keep learning
Related tutorials and quizzes for this topic.