Take the 2-minute tour ×

How can I check if a string value has exact characters in it using Python2? Specifically, I am looking to detect if it has dollar signs ("$"), commas (","), and numbers.

share|improve this question
Does that mean every character is supposed to be one of these, or does it suffice that one (or all) of these characters is present in the string? Do they have to be in some order (eg: $2,00) for it be valid? –  NullUserException Mar 4 '11 at 2:11

3 Answers 3

up vote 57 down vote accepted

Assuming your string is s:

'$' 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.

... or

pattern = re.compile(r'\d\$,')
if pattern.findall(s):
    print('Not found')

... or

chars = set('0123456789$,')
    print('Not Found')

[Edit: added the '$' in s answers]

share|improve this answer
s.find('$')!=-1 => '$' in s :-) –  Jochen Ritzel Mar 4 '11 at 2:25
@Jochen: well... uh... yes :) –  dappawit Mar 4 '11 at 2:26
Is there any particular reason why value on not found was kept -1 and not 0 ?? –  akki Apr 12 '14 at 19:16
@akki not found is -1 because 0 is the index of the first character in a string. Thus "abc".find('a') = 0. It would be ambiguous if 0 was also the not found value. –  lemiant Apr 17 '14 at 14:54
ok.I thought the function just checks if a character is in a string and returns some boolean value, guess the function does much much more than that..Thanks. –  akki Apr 17 '14 at 17:43

user Jochen Ritzel said this in a comment to an answer to this question from user dappawit. It should work:

('1' in var) and ('2' in var) and ('3' in var) ...

'1', '2', etc. should be replaced with the characters you are looking for.

See this page in the Python 2.7 documentation for some information on strings, including about using the in operator for substring tests.

Update: This does the same job as my above suggestion with less repetition:

# When looking for single characters, this checks for any of the characters...
# ...since strings are collections of characters
any(i in '<string>' for i in '123')
# any(i in 'a' for i in '123') -> False
# any(i in 'b3' for i in '123') -> True

# And when looking for subsrings
any(i in '<string>' for i in ('11','22','33'))
# any(i in 'hello' for i in ('18','36','613')) -> False
# any(i in '613 mitzvahs' for i in ('18','36','613')) ->True
share|improve this answer
+1 this is more compact than multiple .find()'s, and is fine as long as the number of characters searched for is low. Doesn't need the parentheses though. –  Sean Feb 15 '13 at 8:46
@Sean About the parenthenses: I know, however it is easier for me to always use them, than to always remember the precedence order :-). –  Abbafei Feb 17 '13 at 7:46

This will test if strings are made up of some combination or digits, the dollar sign, and a commas. Is that what you're looking for?

import re

s1 = 'Testing string'
s2 = '1234,12345$'

regex = re.compile('[0-9,$]+$')

if ( regex.match(s1) ):
   print "s1 matched"
   print "s1 didn't match"

if ( regex.match(s2) ):
   print "s2 matched"
   print "s2 didn't match"
share|improve this answer
Copy/paste error. Thanks. –  ajwood Mar 4 '11 at 2:16
You don't have to escape the $ if it's in a character class. Also this will match 'testing $tring', which I don't think is something the OP wants to happen. –  NullUserException Mar 4 '11 at 2:20
If I recall correctly, it wouldn't match 'testing $tring' it if the match method is used, only if search is used. So I think his code is fine. –  dappawit Mar 4 '11 at 2:22
@dappa It will still match '$string' though –  NullUserException Mar 4 '11 at 2:31
@NullUser... True –  dappawit Mar 4 '11 at 2:34

Your Answer


