+ 2

How can I find special characters (like /) in a string in python?

25th Dec 2022, 3:07 PM
Gaurav Kapoor
Gaurav Kapoor - avatar
4 Réponses
+ 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?
25th Dec 2022, 5:29 PM
Lothar
Lothar - avatar
+ 3
N = "/" if "/" in N: print(True) — if you want to find special characters use \ like (if "\\" in N) to find \ in N
25th Dec 2022, 3:25 PM
Lwez
Lwez - avatar
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")
27th Dec 2022, 2:15 PM
Purva
Purva - avatar
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)
27th Dec 2022, 2:18 PM
Purva
Purva - avatar