Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Count Uppercase Characters

Count uppercase characters using isupper. Students test characters individually and counting uppercase letters from asking whether an entire string is uppercase.

BeginnerPhase 5 - Category 2: Character OperationsExample 4 of 10
count-uppercase-characters.py
1# Take string input
2s = input("Enter a string: ")
3
4# Count uppercase
5count = 0
6for char in s:
7 if char.isupper():
8 count += 1
9
10print(f"Uppercase characters: {count}")

Output

Enter a string: Hello World
Uppercase characters: 2

In this one

  • Test uppercase characters
  • Count without transforming
  • Exclude uncased characters

How it works

  1. 1

    Initialize count zero.

  2. 2

    Call isupper on each char.

  3. 3

    Increment and print the number of matches.

Edge cases

Do digits count?

Do digits count?

No. A single digit is not uppercase.

What does empty input count as?

What does empty input count as?

Zero uppercase characters.

What's going on

Each char is checked with isupper. A true result adds one to the counter; no character is changed.

In AbC1, A and C count while b and 1 do not. Because the test is per character, a mixed-case string can still contain uppercase matches. Unicode uppercase characters may also qualify; this is not restricted to ASCII A through Z.

Do digits count? No. A single digit is not uppercase.

What does empty input count as? Zero uppercase characters.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is counted for AbC1?

Pick an answer to check it.

Question 2

Do digits count?

Pick an answer to check it.

Questions people ask

Do digits count?

No. A single digit is not uppercase.

What does empty input count as?

Zero uppercase characters.

What is counted for AbC1?

Two.