Coders Packet

Character Counter using Python

By Vishaka Iyengar

This character counter can be used to count the number of characters, words, sentences and blank lines in the text given as input.

This code is useful where there are strict word and character limits. It could be in an essay or a Twitter post or an Instagram bio that mention a limit to the word count but do not display the amount of content written. 

The code is simple and straight forward. It does not require any new modules or installations. 

Understanding this code requires the basic concepts of strings, lists and loops.                                                                                                              

The code has been explained with comments within it. 

text = input("Input your text here:")

characters = len(text)
print("There are ", characters,"characters in your text.")

word = text.split()
print("There are ",len(word), "words in your text.")

lines = 0
blanklines = 0
sentences = 0
for line in text:
    #print(line)
    lines = lines + 1
    if line.startswith('\n'):
        blanklines = blanklines + 1
    else:
        # We assume that each sentence ends with . or ! or ? So we count those characters.
        sentences = sentences + ( line.count('.') + line.count('!') + line.count('?') )
        # None is used to count spaces. Even double spaces will count as a single space.
        tempwords = line.split(None)
        #print (tempwords)
print("There are ", sentences, "sentences in your text.")
print("There are ", blanklines, "blanklines in your text.")

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Vishaka Iyengar (reachvishakas)

Download packets of source code on Coders Packet