Python Practice › String Programs › Program 36

Does a string have special characters?

EasyString Programs · 36 of 106

Write a Python program to check whether a string contains special characters.

Advertisement
Input
"python$$#sqatools", "python"
Output
python$$#sqatools -> has special characters ['$', '$', '#']
python -> no special characters []

Approach

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

Solution

Python
for text in ["python$$#sqatools", "python"]:    special = [ch for ch in text if not ch.isalnum() and not ch.isspace()]    print(text, "->", "has special characters" if special else "no special characters", special)
Output
python$$#sqatools -> has special characters ['$', '$', '#']
python -> no special characters []

Learn the concept

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

More string programs

← All 106 String Programs

Leave a Comment