Hema Latha
Hema Latha

Reputation: 185

Check special character in string?

i need to check existing of Character (*,&,$) in Given String using python command such as given below?

Eg: stringexample='mystri$ng&*'

check stringexample contains any special symbols like *,&,$ then return true? shall i try with string method __contains__()

Upvotes: 1

Views: 8869

Answers (2)

eumiro
eumiro

Reputation: 212885

if any(c in stringexample for c in '*$&'):
    ...

or

if not set('*&$').isdisjoint(stringexample):
    ...

Upvotes: 0

taskinoor
taskinoor

Reputation: 46027

>>> stringexample = 'mystri$ng&'
>>> '*' in stringexample
False
>>> '$' in stringexample
True
>>> '&' in stringexample
True
>>>

Upvotes: 1

Related Questions