Ask Question
8 January, 14:52

LAB: Count input length without spaces, periods, or commas

Given a line of text as input, output the number of characters excluding spaces, periods, or commas. You may assume that the input string will not exceed 50 characters.

Ex: If the input is:

Listen, Mr. Jones, calm down.

the output is:

21

Note: Account for all characters that aren't spaces, periods, or commas (Ex: "r", "2", "!").

+3
Answers (1)
  1. 8 January, 17:51
    0
    import re

    str1 = input ("Enter the sentence less than 50 characters: ")

    pattern = '[a-zA-Z]'

    count=0

    i=0

    while (i<=len (str1) - 1):

    result = re. match (pattern, str1)

    if (result):

    count+=1

    else:

    continue

    i+=1

    print ("String Length:", count)

    Explanation:

    I have used here the re library that is meant for the regular expressions in Python.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “LAB: Count input length without spaces, periods, or commas Given a line of text as input, output the number of characters excluding spaces, ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers