Python Tutorial | Python Data File Handling 7

Question: 6Python program to read the contents of the file “story.txt”. Further, count and print total digit in the file.Sol:

```def count_digit():
d=0
with open("story.txt") as f:
while True:
if not c:
break
print(c,end='')
if(c>='0' and c<='9'):
d=d+1
print("total digits  ",d)
#function calling
count_digit()```

Question: 7Python program to read the contents of the file “story.txt”. Further, count and print total vowels in the file.Sol:

```def count_vowels():
v=0
vowels="aeiouAEIOU"
with open("story.txt") as f:
while True:
if not c:
break
print(c,end='')
if(c in vowels):
v=v+1
print("total vowels  ",v)
function calling
count_vowels()```

Question : 8Python program to read the contents of the file “story.txt”. Further count and print total consonants in the file.Sol:

```def count_consonants():
c1=0
vowels="aeiouAEIOU"
with open("story.txt") as f:
while True:
if not c:
break
print(c,end='')
if((c>='A' and c<='Z') or(c>='a' and c<='z')):
if not (c in vowels):
c1=c1+1
print("total consonants ",c1)
function calling
count_consonants()```

Question : 9Python program to read the contents of the file “story.txt”. Further count and print the following:#total length#total alphabets#total vowels#total conbsonants#total non alpha charactersSol:

```def count():
v=0 #vowels
le=0 #len
c1=0 # consonents
c2=0 # non alphabetic characters
a=0
vowels="aeiouAEIOU"
with open("story.txt") as f:
while True:
if not c:
break
print(c,end='')
le=le+1
if((c>='A' and c<='Z') or(c>='a' and c<='z')):
a=a+1
if (c in vowels):
v=v+1
else:
c1=c1+1
c2=c2+1
print("total vowels  ",v)
print("total consonants ",c1)
print("total alphabets ",a)
print("total chars which are not alphabets ",c2)
print("length = ",le)
function calling
count()```

Question: 10Python program to read the contents of the file “story.txt”. Further, count and print total spaces in the file.Sol:

```def count_space():
s=0
space=" "
with open("story.txt") as f:
while True:
if not c:
break
print(c,end='')
if(c in space):
s=s+1
print("total spaces  ",s)
function calling
count_space()```

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling

Important Pages