[Tutor] if string contains dictionary key

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Thu, 10 Aug 2000 12:37:10 -0700 (PDT)


On Thu, 10 Aug 2000, Steve wrote:

> I know I am missing something obvious, but I am having a difficult 
> time trying to get this to work.
> 
> s = 'This string contains a CLR'
> 
> d = {'CLR ': 'CLEAR', 'CA': 'ACCEPT', 'CR': 'CALL REQUEST'}
> 
> 
> if s has d.keys():
>    do stuff


'has', unfortunately, isn't a keyword.  You probably mean 'in' instead

  if s in d.keys():
    do stuff

Because 'in' does an exact check, 's' won't match with anything in 'd' in
your example.