+ 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.

23rd Jul 2020, 8:14 AM
Jaya Singh
Jaya Singh - avatar
17 ответов
+ 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
23rd Jul 2020, 10:52 AM
Lothar
Lothar - avatar
+ 7
print(any([x in string for x in"#@$*%!&"]))
23rd Jul 2020, 9:45 AM
Oma Falk
Oma Falk - avatar
+ 5
Jaya Singh Use regex (regular expression)
23rd Jul 2020, 8:17 AM
A͢J
A͢J - avatar
+ 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..👌👌😃😃
23rd Jul 2020, 8:20 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 4
Lothar very fine contribution
23rd Jul 2020, 11:21 AM
Oma Falk
Oma Falk - avatar
+ 3
some of them or all?
23rd Jul 2020, 8:18 AM
Oma Falk
Oma Falk - avatar
+ 3
♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ thank you so much. I understood now. I was little bit confused, now it's clear.
23rd Jul 2020, 9:32 AM
Jaya Singh
Jaya Singh - avatar
+ 3
s=input() for i in range(len(s)): ch=s[i] if ch in "#@$*%!&": You can move further with the code.
23rd Jul 2020, 8:21 PM
Ayush Rastogi
Ayush Rastogi - avatar
23rd Jul 2020, 8:20 AM
Sâñtôsh
Sâñtôsh - avatar
+ 2
♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ But isalpha, isnum is for checking the letters and numbers, not special characters.
23rd Jul 2020, 9:18 AM
Jaya Singh
Jaya Singh - avatar
+ 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
25th Jul 2020, 6:50 AM
Ishmam
Ishmam - avatar
23rd Jul 2020, 9:09 AM
Jaya Singh
Jaya Singh - avatar
+ 1
Ethien thank you for the suggestion 🙌🙌
23rd Jul 2020, 9:09 AM
Jaya Singh
Jaya Singh - avatar
+ 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
24th Jul 2020, 5:13 AM
Divya Mohan
Divya Mohan - avatar
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.
23rd Jul 2020, 9:00 AM
Jaya Singh
Jaya Singh - avatar
0
Oma Falk I have to check whether any of these special characters are present or not
23rd Jul 2020, 9:01 AM
Jaya Singh
Jaya Singh - avatar
0
All you need in life is confidence then success is sure!!!
24th Jul 2020, 2:38 AM
Ayuba Kabiru
Ayuba Kabiru - avatar