Python Practice › String Programs › Program 36
Does a string have special characters?
Write a Python program to check whether a string contains special characters.
Input
"python$$#sqatools", "python"
Output
python$$#sqatools -> has special characters ['$', '$', '#'] python -> no special characters []
Approach
- A special character is not alphanumeric and not a space.
Solution
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.