Knowee
Questions
Features
Study Tools

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

Question

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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python program that reads a file and prints the number of lines, words, and characters in the file:

def count_file_content(file_name):
    with open(file_name, 'r') as file:
        lines = file.readlines()
        sentences = len(lines)
        words = sum(len(line.split()) for line in lines)
        chars = sum(len(line) for line in lines)
    print(f"Sentences: {sentences}")
    print(f"Words: {words}")
    print(f"Chars: {chars}")

# Test Case 1
count_file_content('input1.txt')

# Test Case 2
count_file_content('input2.txt')

This program works by opening the file in read mode, then reading all lines into a list. The number of sentences is the length of this list. The number of words is the sum of the lengths of the lists produced by splitting each line on spaces. The number of characters is the sum of the lengths of each line.

Please replace 'input1.txt' and 'input2.txt' with the actual path of your files.

This problem has been solved

Similar Questions

Find the count of letters and words in a file.

Problem StatementWrite a program to count the lines, words and characters in a given text.Input FormatInput the text (Ctrl+D on a new line to end input)Output FormatPrint the no of lines, words, charactersSample Input 1This is a sample text.It has multiple lines.Each line contains words.Sample Output 1Lines: 3Words: 12Characters: 80Sample Input 2Hello WorldHappy CodingSample OutputLines: 2Words: 4Characters: 23

Print the number of lines, words, and characters in a file.Write a program to print the number of lines, words, and characters in a file.Note: Take the input file name from the user.Sample Test CasesTest Case 1:Expected Output:Enter·file·name:·InputData1.txtLine·count·=·7Word·count·=·51Char·count·=·306

Write a python program to count the number of characters of alphabets 'O' and 'i' from the given file

Select the correct implementation of a piece of code that accepts a sentence as input and prints the number of words in it. A sentence is just a sequence of words with a space between consecutive words. You can assume that the sentence will not have any other punctuation marks. You can also assume that the input string will have at least one word.1sentence = input()2space = ' ' # there is a single space between the quotes3num_words = sentence.count(space)4print(num_words)1sentence = input()2space = ' ' # there is a single space between the quotes3num_words = sentence.count(space) + 14print(num_words)1sentence = int(input())2space = ' ' # there is a single space between the quotes3num_words = sentence.count(space)4print(num_words)1sentence = input()2num_words = len(sentence)3print(num_words)

1/3

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.