197. Python Program to Count Uppercase, Lowercase, Digits and Spaces
EasyPhase 1 · Warm-up
You know for loops, in and isdigit(). That is all you need here.
Overview
Counting the kinds of characters in a text is a common interview warm-up. Forms and password checkers do the same thing every day.
Problem Statement
Mateo builds a sign-up form for a website. Mateo wants to know what kinds of characters people type into it.
Read one line of text. Count the uppercase letters (A to Z), the lowercase letters (a to z), the digits (0 to 9) and the spaces. Other characters, like , or ., are not counted.
Steps
- Read the line of text.
- Make four counters that start at 0.
- Loop over the characters. Add 1 to the right counter: uppercase, lowercase, digit or space.
- Print the four counts, one per line, in the order shown below.
Examples
Example 1. Input Hello World 2024 gives:
Uppercase: 2
Lowercase: 8
Digits: 4
Spaces: 2
Why: H and W are uppercase. e, l, l, o, o, r, l and d are 8 lowercase letters. 2024 is 4 digits, and there are 2 spaces.
Example 2. Input Hi, Tom. gives:
Uppercase: 2
Lowercase: 3
Digits: 0
Spaces: 1
Why: H and T are uppercase, and i, o, m are lowercase. The , and the . are not counted.
Input
One line of text (1 character or more), with English letters, digits, spaces and maybe other signs. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
Four lines, in this order:
Uppercase: <count>
Lowercase: <count>
Digits: <count>
Spaces: <count>
The tests try 6 different inputs.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.