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
Find the count of letters and words in a file.
Count the sentences, words and characters in a fileWrite a Python program that reads a file and prints the number of lines, words, and characters in the file.Input Format:The input will be a single string, which is the name of the file to be read.Output Format:The output will be three separate lines, each containing a count:The first line will contain the number of lines in the file, prefixed with "Sentences: ".The second line will contain the number of words in the file, prefixed with "Words: ".The third line will contain the number of characters in the file, prefixed with "Chars: ".Sample Test CasesTest Case 1:Expected Output:file·name:input1.txtSentences:·8Words:·34Chars:·228Test Case 2:Expected Output:file·name:input2.txtSentences:·7Words:·51Chars:·306
Write a program that accepts an input string from the user and prints the total number of alphabet characters (A-z) in the string to the output.
Write a program to build a histogram using dictionary to count the number oftimes each character appears in words starting with a certain character.The program• asks the user to input a filename and a character• builds a histogram using dictionary to count the number of times eachcharacter appears in those words in the file starting with the input character• prints the characters in alphabetical order and in lower case and itsfrequency, separated by a single space, one row per character ended with anewline• if no word starts with the input character, print “Not found
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.