๐Ÿ
B.TECH I YEAR โ€ข II SEMESTER โ€ข R25

Python Programming - Lab Manual

Python Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R25 Regulation ๐ŸŽ“ I Year ๐Ÿ“š II Semester ๐Ÿ Python Programming ๐Ÿงช Lab Program
Selected Program

single letter words

โ† All Python Programs

Aim:

☛   The wordlist I provided, words.txt, doesn't contain single letter words. So you might want to add "I", "a", and the empty string.

Solution :


Text File:words.txt

apple
banana
cat
dog
elephant

PROGRAM: (single_letter.py)

 
def load_words(filename):
	with open(filename, "r") as file:
		words = [line.strip() for line in file]

	# Add missing single-letter words and empty string
	if "" not in words:
		words.append("")
	if "I" not in words:
		words.append("I")
	if "a" not in words:
		words.append("a")
	
	return words
# Sample usage
words = load_words("words.txt")
print("Total number of words:", len(words))
print("Sample words:", words[:10])

OUTPUT:

 
Total number of words: 8
Sample words: ['apple', 'banana', 'cat', 'dog', 'elephant', '', 'I', 'a']



๐Ÿ’ป Run This Python Program

single letter words
The selected Python program is loaded automatically into the editor. Edit the program, provide input if required and click Run.

The compiler is embedded using OneCompiler. Your Python program runs inside the embedded Python environment; Study Glance does not execute Python code directly on your web server.
โ† Back to Python Lab Programs ๐Ÿ“š All Lab Manuals