It is necessary to check whether there are special characters in the line (.,:;! _ * - + () / # ¤% &).
For example, is there a "booster!" at least one special character from the list above.
|
3 answers
To find out if character sets intersect:
>>> not set(".,:;!_*-+()/#¤%&)").isdisjoint("Booster!") True set.isdisjoint finds out if the sets are unbound.
You can use regular expressions:
>>> import re >>> has_special = re.compile("|".join(map(re.escape, ".,:;!_*-+()/#¤%&)"))).search >>> bool(has_special("Booster!")) True Or a simple loop (quadratic algorithm):
>>> text = "Booster!" >>> any(char in ".,:;!_*-+()/#¤%&)" for char in text) True |
More general case:
import string print string.punctuation #выводит !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ then through replace () you can delete what you don’t need
Getting these special characters:
s = ".,:;!_*-+()/#¤%&" my_string="Booster!" set(s)&set(s1) # выведет общие символы, то есть ! len(set(s)&set(s1))>0 # выведет True s = ".,:;!_*-+()/#¤%&" my_string="Booster!" any(x for x in s if x in my_string) # выведет True, если есть общие спецсимволы. |
You can use the if x in y construction: Here, for example:
def strng(word): s = ".,:;!_*-+()/#%&" for char in s: if char in word: print "The character " + char + " is in the word." else: pass strng("Boost!") |