Posted By: Anonymous
How can I check if a string has several specific characters in it using Python 2?
For example, given the following string:
The criminals stole $1,000,000 in jewels.
How do I detect if it has dollar signs (“$”), commas (“,”), and numbers?
Assuming your string is
'$' in s # found '$' not in s # not found # original answer given, but less Pythonic than the above... s.find('$')==-1 # not found s.find('$')!=-1 # found
And so on for other characters.
pattern = re.compile(r'd$,') if pattern.findall(s): print('Found') else print('Not found')
chars = set('0123456789$,') if any((c in chars) for c in s): print('Found') else: print('Not Found')
[Edit: added the
'$' in s answers]