In this program, we will find the total number of special characters in a string.
Steps to solve the program
- Take a string as input and create another string of special characters.
- Create a variable to count the total number of special characters in a string.
- Use for loop to iterate over each character of the input string.
- If that character is in the string containing special characters then add 1 to the variable that we have created.
- Print the output.
str1 = '@sqa#tools!!'
special_char = '!@#$%^&*()_{[}]'
special = 0
for char in str1:
if char in special_char:
special += 1
print("Special character: ",special)
Output :
Special character: 4