๐Ÿ
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

displays the number of words, number of vowels, blank spaces, lower case letters and uppercase letters

โ† All Python Programs

Aim:

☛   Write a function that reads a file file1 and displays the number of words, number of vowels, blank spaces, lower case letters and uppercase letters.

Solution :


Input Data file ("search.txt"):


Hello python
this is study glance
123 456

PROGRAM: (counting.py)

 
def counting(filename):
	txt_file = open(filename , "r")
	no_of_vowels = 0
	no_of_words = 1
	no_of_lines = 1
	no_of_spaces = 0
	no_of_uppercase = 0
	no_of_lowercase = 0
	no_of_digits = 0
	# Make a vowels list
	vowels_list = ['a','e','i','o','u','A','E','I','O','U']
	# Iterate over the characters present in file
	for ch in txt_file.read():
		if ch == "\n" or ch == ' ':
			no_of_words = no_of_words + 1
		if ch == ' ':
			no_of_spaces = no_of_spaces + 1
		if ch in vowels_list:
			no_of_vowels = no_of_vowels + 1
		if ch == "\n":
			no_of_lines = no_of_lines + 1
		if(ord(ch) >= 97 and ord(ch) <= 122):
			no_of_lowercase = no_of_lowercase + 1
		if(ord(ch) >= 65 and ord(ch) <= 90):
			no_of_uppercase = no_of_uppercase + 1
		if(ord(ch) >= 48 and ord(ch) <= 57):
			no_of_digits = no_of_digits + 1
	# Print the desired output on the console.
	print(" Number of vowels = ", no_of_vowels)
	print(" Number of words = " , no_of_words)
	print(" Number of Blank spaces = " , no_of_spaces)
	print(" New Lines = " , no_of_lines)
	print(" Number of lower case characters in = " , no_of_lowercase)
	print(" Number of upper case characters = " , no_of_uppercase)
	print(" Number of digits = " , no_of_digits)
# Driver Code
filename = input(" Enter text file name = ")
counting(filename)

OUTPUT:

 
 Enter text file name = search.txt
 Number of vowels =  8
 Number of words =  8
 Number of Blank spaces =  5
 New Lines =  3
 Number of lower case characters in =  27
 Number of upper case characters =  1
 Number of digits =  6


๐Ÿ’ป Run This Python Program

displays the number of words, number of vowels, blank spaces, lower case letters and uppercase letters
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