+ 4
How to check whether the special characters are present in a string?
I have to check the given special characters (#,@,$,*,%,!,&) in a string. String also includes letters and digits.
21 Answers
+ 9
I personally like Oma Falk's code. It is very pythonic, very clear and straight forward, and it has a good readability.
I have made some extra additions for:
- using pre-defined string for special characters
- giving a list with all special characters found (with duplicates)
- giving a list with all special characters found (without duplicates)
- giving the total count of special characters found
https://code.sololearn.com/cSxNqbuy31ev/?ref=app
+ 7
print(any([x in string for x in"#@$*%!&"]))
+ 5
Jaya Singh Use regex (regular expression)
+ 4
I encourage you to give it a try first, then if you still can't solve the issue, attach your try and we'd point you in the right direction.
Directions infact..👌👌😃😃
+ 4
Lothar very fine contribution
+ 3
some of them or all?
+ 3
♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ thank you so much. I understood now. I was little bit confused, now it's clear.
+ 3
s=input()
for i in range(len(s)):
ch=s[i]
if ch in "#@$*%!&":
You can move further with the code.
+ 2
Read that tutorial 👇👇
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2476/
+ 2
♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ But isalpha, isnum is for checking the letters and numbers, not special characters.
+ 2
I don't know why people think regex is hard, but I think it becomes ridiculously simple once you get the concept.
By the way, here's the code:
import re
string = "jhgr#66£"
ptrn = re.compile(r"[@#$!%&*]")
print(bool(ptrn.search(string)))
https://code.sololearn.com/ctmbRc843HKF/?ref=app
+ 1
♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ thank you, I will try this also 🙌
+ 1
Ethien thank you for the suggestion 🙌🙌
+ 1
use regex it works in all languages.
example in js .
i don't know python abc but regex works everywhere.
https://code.sololearn.com/We0i7975Y6m1/?ref=app
0
Tomiwa Joseph I have tried it. Actually, I have asked this question regarding the code coach of password validation. In this I am facing problem regarding special characters.
0
Oma Falk I have to check whether any of these special characters are present or not
0
All you need in life is confidence then success is sure!!!