Count Uppercase Characters
Count uppercase characters using isupper. Students test characters individually and counting uppercase letters from asking whether an entire string is uppercase.
1# Take string input2s = input("Enter a string: ")34# Count uppercase5count = 06for char in s:7 if char.isupper():8 count += 1910print(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
Initialize count zero.
- 2
Call isupper on each char.
- 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.
๐Related Content
- ๐
Phase 5 - Learn Concepts
Review Phase 5 concepts and explanations
- ๐
Phase 5 - Quiz
Test your Phase 5 understanding with quiz questions
- ๐ป
Phase 5 - All Practice Problems
Explore all practice problems for Phase 5
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum