VOOZH about

URL: https://www.geeksforgeeks.org/python/python-finding-n-character-words-in-a-text-file/

⇱ Finding 'n' Character Words in a Text File - Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Finding 'n' Character Words in a Text File - Python

Last Updated : 12 Jan, 2026

Given a text file, the task is to find all words that are exactly ā€˜n’ characters long. This can be useful in text analysis, data cleaning, word filtering, or pattern-matching applications.

Example: (Myfile.txt)

Hello, how are you? This is a simple text.

Input: n=3
Output: ['how', 'are', 'you']

Using Regular Expressions

Regular expressions allow you to match patterns in text efficiently. Here we can use \b\w{n}\b to find words of exact length n.

Output:

['how', 'are', 'you']

Explanation:

  • text = f.read(): Reads entire file content.
  • pattern = r'\b\w{n}\b': Matches words with exactly n characters (\b ensures word boundaries) (\w{n} ensure any word of length n)
  • re.findall(pattern, text): Returns a list of all matching words.

Using Split and List Comprehension

This method splits text into words and filters words based on length using list comprehension.

Output:

['how', 'are', 'you']

Explanation:

  • text.split(): Splits the text into words.
  • [w for w in w1 if len(w) == n]: Filters words with length exactly n.

Using a Generator Function

Generators yield words one by one, making them memory-efficient for very large files.

Comment