Python | Finding 'n' Character Words in a Text File
Last Updated : 04 Aug, 2023
This article aims to find words with a certain number of characters. In the code mentioned below, a Python program is given to find the words containing three characters in the text file.
Example
Input: Hello, how are you ? , n=3 Output: how are you Explanation: Output contains every character of the of length 3 from the input file.
Input File: File1.txt
File1.txtPython Program to Find 'n' Character Words in a Text File
Below are the methods that we will cover in this article:
Finding a Given Word in Text File using Split and List Comprehension
You can read the file, split its content into words, and then filter out words with 'n' characters using list comprehension.
PYTHON def find_words_with_three_chars(file_path): with open(file_path, 'r') as file: content = file.read() words = content.split() words_with_three_chars = [word for word in words if len(word) == 3] return words_with_three_chars file_name = "File1.txt" result = find_words_with_three_chars(file_name) print("Words containing three characters:") print(result)
Output:
Words containing three characters: ['sit', 'sed', 'sit', 'ac.', 'dui', 'sit']
Time Complexity: O(N), where N is the total number of characters in the file.
Space Complexity: O(N + M), where M is the number of words with 'n' characters in the file.
Search a given word in a Text File using Regular Expressions
In Python, we have re
module which provides regular expression functionality. The Regular expressions can be used to match patterns in the text which allows you to find words with 'n' characters.
Python3 import re def find_n_character_words(file_path, n): with open(file_path, 'r') as file: content = file.read() # Use regular expression to find words with 'n' characters pattern = r'\b\w{' + str(n) + r'}\b' words_with_n_chars = re.findall(pattern, content) return words_with_n_chars file_name = "File1.txt" n = 3 result = find_n_character_words(file_name, n) print(f"Words containing {n} characters:") print(result)
Output:
Words containing 3 characters: ['sit', 'sed', 'sit', 'dui', 'sit']
Time Complexity: O(N), where N is the total number of characters in the file.
Space Complexity: O(N)
Get Words Containing three Characters in the File using Generator Function
Here you can use a generator function to yield words with 'n' characters one by one. This can be useful for large files.
Python3 def generate_n_character_words(file_path, n): with open(file_path, 'r') as file: for line in file: for word in line.split(): if len(word) == n: yield word file_name = "File1.txt" n = 3 result_generator = generate_n_character_words(file_name, n) print(f"Words containing {n} characters:") for word in result_generator: print(word)
Output:
Words containing 3 characters: sit sed sit dui sit
Time Complexity: O(N), where N is the total number of characters in the file.
Space Complexity: O(M), where M is the number of words with 'n' characters in the file.
Similar Reads
Count Words in Text File in Python Our task is to create a Python program that reads a text file, counts the number of words in the file and prints the word count. This can be done by opening the file, reading its contents, splitting the text into words, and then counting the total number of words.Example 1: Count String WordsFirst,
3 min read
Count Vowels, Lines, Characters in Text File in Python We are going to create a Python program that counts the number of vowels, lines, and characters present in a given text file.Example:Assume the content of sample_text.txt is:Hello, this is a sample text file.It contains multiple lines.Counting vowels, lines, and characters.OutputTotal number of vowe
2 min read
Get number of characters, words, spaces and lines in a file - Python Given a text file fname, the task is to count the total number of characters, words, spaces, and lines in the file. As we know, Python provides multiple in-built features and modules for handling files. Let's discuss different ways to calculate the total number of characters, words, spaces, and line
5 min read
Check If a Text File Empty in Python Before performing any operations on your required file, you may need to check whether a file is empty or has any data inside it. An empty file is one that contains no data and has a size of zero bytes. In this article, we will look at how to check whether a text file is empty using Python.Check if a
4 min read
Find position of a character in given string - Python Given a string and a character, our task is to find the first position of the occurrence of the character in the string using Python. For example, consider a string s = "Geeks" and character k = 'e', in the string s, the first occurrence of the character 'e' is at index1. Let's look at various metho
2 min read