Write a program that takes in a sentence as input and displays the number of words, number of capital letters, no. of small letters and number of special symbols.

st = input("Enter Sentence:")
wc = cl_c = sl_c = nc = sp_c = 0
l = len(st)
for i in range(l):
    if st[i] == ' ':
        wc += 1
    elif st[i] >= 'A' and st[i] <= 'Z':
        cl_c += 1
    elif st[i] >= 'a' and st[i] <= 'z':
        sl_c += 1
    elif st[i] >= '0' and st[i] <= '9':
        nc += 1
    else:
        sp_c += 1

print('The number of words =', wc)
print('The number of capital letters =', cl_c)
print('The number of small letters =', sl_c)
print('The number of special symbols =', sp_c)
Output :
Enter Sentence: Hello World! 123
Words: 2 (Hello and World!)
Capital letters: 2 (H and W)
Small letters: 8 (e, l, l, o, o, r, l, and d)
Special symbols: 2 (! and space)

Sure! This Python code takes a sentence as input and then counts various properties of the characters in that sentence, such as the number of words, capital letters, small letters, and special symbols. Here’s how it works:

Explanation

  1. Input Sentence:
   st = input("Enter Sentence:")
  • This line prompts the user to enter a sentence and stores it in the variable st.
  1. Initialize Counters:
   wc = cl_c = sl_c = nc = sp_c = 0
  • wc: Counter for words (initialized to 0).
  • cl_c: Counter for capital letters (initialized to 0).
  • sl_c: Counter for small letters (initialized to 0).
  • nc: Counter for numbers (initialized to 0).
  • sp_c: Counter for special symbols (initialized to 0).
  1. Iterate Through Characters:
   l = len(st)
   for i in range(l):
  • This loop iterates through each character in the input sentence st.
  1. Character Classification:
  • If the character is a space ' ', it’s counted as a word delimiter, so wc is incremented.
  • If the character is uppercase ('A' to 'Z'), cl_c is incremented.
  • If the character is lowercase ('a' to 'z'), sl_c is incremented.
  • If the character is a digit ('0' to '9'), nc is incremented.
  • Otherwise, it’s considered a special symbol, and sp_c is incremented.
  1. Print Results:
   print('The number of words =', wc)
   print('The number of capital letters =', cl_c)
   print('The number of small letters =', sl_c)
   print('The number of special symbols =', sp_c)
  • After the loop, the code prints the counts of words, capital letters, small letters, and special symbols.

Example

Let’s say the user inputs the following sentence:

Enter Sentence: Hello World! 123
  • The code will count:
  • Words: 2 (Hello and World!)
  • Capital letters: 2 (H and W)
  • Small letters: 8 (e, l, l, o, o, r, l, and d)
  • Special symbols: 2 (! and space)

Summary

This code is a simple Python program to count various properties of characters in a given sentence, such as the number of words, capital letters, small letters, numbers, and special symbols. It demonstrates basic string manipulation and counting techniques in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *