+ 2
How can I find special characters (like /) in a string in python?
4 Answers
+ 4
Gaurav Kapoor ,
can you give some more details about finding characters in a string:
> do you just need to know if the character is in the string?
> what about this if the search character appears multiple times in the string? do we need to count how many times a certain letter
occurs in a string?
> do you need to know the index position of the search character?
+ 3
N = "/"
if "/" in N:
print(True)
â if you want to find special characters use \ like (if "\\" in N) to find \ in N
0
# To find if a string contains special character or not
s=input('Enter string\n')
found=False
for i in s:
if not (i.isalnum() or i.isspace()):
found=True
if found==True:
print('String contains special characters')
else:
print("String doesn't contain special characters")
0
# To find the no. of special characters in a string
s=input('Enter string\n')
cnt=0
for i in s:
if not (i.isalnum() or i.isspace()):
cnt+=1
print('No. of special characters in ' , s , '=' , cnt)