Ask Question
29 November, 23:37

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", "!").

+1
Answers (1)
  1. 30 November, 02:06
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers