Python Practice › Loop Programs › Program 75
Count special characters
Write a Python program to count the special characters (not letters, digits or spaces) in a string.
Input
"@sqa#tools!!"
Output
4
Approach
- A special character is not alphanumeric and not a space.
Solution
text = "@sqa#tools!!"count = 0for ch in text: if not ch.isalnum() and not ch.isspace(): count += 1print(count)Output
4
Learn the concept
New to this topic? Read the lesson, then come back.