Home > Software engineering >  How to find these symbols in strings in Python? [closed]
How to find these symbols in strings in Python? [closed]

Time:09-28

Im trying to find symbols(@,$,&,! ,-) in strings: ('Give me 5'), ('Monday Friday!') and answer should be True or False. Please help me.

CodePudding user response:

Try this:

>>> symbols = ['@','$','&','!' , ' ','-'] 
>>> strings = ['Give me 5' , 'Monday   Friday!']
>>> [any(sbl in st for sbl in symbols) for st in strings]
[False, True]

CodePudding user response:

returns true if any of the symbols is in the string

for symbol in ():
    if symbol in string:
        return True

return False

returns true if all of the symbols are in the string

for symbol in ():
    if symbol not in string:
        return False

return True
  • Related