Write a python program to count the number of characters of alphabets 'O' and 'i' from the given file
Question
Write a python program to count the number of characters of alphabets 'O' and 'i' from the given file
Solution
Sure, here is a simple Python program that counts the number of 'O' and 'i' characters in a file:
def count_chars(filename):
with open(filename, 'r') as f:
text = f.read()
count_O = text.count('O')
count_i = text.count('i')
return count_O, count_i
filename = 'your_file.txt' # replace with your file name
count_O, count_i = count_chars(filename)
print(f"Count of 'O': {count_O}")
print(f"Count of 'i': {count_i}")
This program works by first opening the file in read mode. It then reads the entire contents of the file into a string using the read
method. The count
method of the string is used to count the occurrences of 'O' and 'i'. The counts are then returned and printed.
Please replace 'your_file.txt'
with the path to the file you want to analyze.
Similar Questions
Write a Python program to count the number of occurrences of a specific character in a string
#Write a Python function count_vowels(text) that takes a string as input and returns the count of vowels (a, e, i, o, u) in the input string, ignoring case.
Write a python program to print the number of occurrences of a substring into a line
Python program that accepts a string and calculate the number of digits and letters and space
Write a short Python function num_vowels(text) that counts the number of vowels ina given character string
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.