Python Practice › String Programs › Program 79

Count uppercase, lowercase, digits and specials

EasyString Programs · 79 of 106

Write a Python program to count the uppercase, lowercase, special and numeric characters in a string.

Advertisement
Input
"@SqaTools.lin"
Output
Special characters: 2
Uppercase characters: 2
Lowercase characters: 9
Numeric values: 0

Approach

  1. Test each character and increase the matching counter.

Solution

Python
text = "@SqaTools.lin"upper = lower = digits = special = 0for ch in text:    if ch.isupper():        upper += 1    elif ch.islower():        lower += 1    elif ch.isdigit():        digits += 1    else:        special += 1print("Special characters:", special)print("Uppercase characters:", upper)print("Lowercase characters:", lower)print("Numeric values:", digits)
Output
Special characters: 2
Uppercase characters: 2
Lowercase characters: 9
Numeric values: 0
Note

“@SqaTools.lin” has 2 special characters (@ and .) and 9 lowercase letters; the original sample showed 1 and 8.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs