Logic Building
# Take string input
s = input("Enter a string: ")
# Count special characters
count = 0
for char in s:
if not char.isalnum() and not char.isspace():
count += 1
print(f"Special characters: {count}")Output
Enter a string: Hello@World! Special characters: 2
Count characters that are not alphanumeric or space.
Key Concepts:
- isalnum() checks alphanumeric
- isspace() checks space
- Count if neither