Python Practice › Loop Programs › Program 75

Count special characters

EasyLoop Programs · 75 of 90

Write a Python program to count the special characters (not letters, digits or spaces) in a string.

Advertisement
Input
"@sqa#tools!!"
Output
4

Approach

  1. A special character is not alphanumeric and not a space.

Solution

Python
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.

More loop programs

← All 90 Loop Programs

Leave a Comment