Ask Question
16 December, 22:39

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

+5
Answers (1)
  1. 16 December, 22:42
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers