Re: finding an element in a string

2007-06-25 Thread Will Maier
On Mon, Jun 25, 2007 at 04:09:50PM -0400, Miguel Oliveira wrote:
> I want to make an if statement in which I would like to find a
> certain word in a sentence; here is what i have so far:
> 
>x = raw_input("how are you?")
> 
>if x == "fine":
>  print "Good."
> 
> But that, obviously, will only respond "good" when one writes
> "fine". I was looking for a way for the program to respond "good"
> to any sentence that would contain the word "fine" in it.

Since strings are sequences, too, you can use the membership
operator ('in'):

>>> input = "spam; spam, eggs and spam; spam, sausage and spam..."
>>> if "spam" in input:
>>>sing()

http://docs.python.org/ref/sequence-types.html

-- 

[Will [EMAIL PROTECTED]|http://www.lfod.us/]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding an element in a string

2007-06-25 Thread Jerry Hill
On 6/25/07, Miguel Oliveira <[EMAIL PROTECTED]> wrote:
> I was looking for a way for the program to respond "good" to any sentence
> that would contain the word "fine" in it.

This will work for any string with the string "fine" in it.

if "fine" in x:
print "Good."

-- 
Jerry
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding an element in a string

2007-06-25 Thread Stephen R Laniel
On Mon, Jun 25, 2007 at 04:09:50PM -0400, Miguel Oliveira wrote:
> But that, obviously, will only respond "good" when one writes "fine". I was
> looking for a way for the program to respond "good" to any sentence that would
> contain the word "fine" in it.

What you want is a regular-expression match. Are you
familiar with regexes? The relevant Python documentation is
here (for the module 're'):
http://docs.python.org/lib/module-re.html

though I find it's not particularly easy reading. The
Friedl book recommended in there is the canonical source
about regexes, and it is indeed a great book. You should
read some manpages first, though. Just google for 'regular
expressions' and you'll find all that you need.

In the meantime, what you want is something like

if re.search( r'fine', x ):
# things one does if the user said 'fine'

Alternately, you could just use rfind():
http://docs.python.org/lib/string-methods.html

but regexes are a good tool to get to know. They're overused
(particularly in Perl), but this is just the sort of task
they're good for.

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


finding an element in a string

2007-06-25 Thread Miguel Oliveira
Hi,
  I was wondering if you could help me with this:
  I want to make an if statement in which I would like to find a certain word in a sentence; here is what i have so far:
 
x = raw_input("how are you?")
 
if x == "fine":
  print "Good."
 
But that, obviously, will only respond "good" when one writes "fine". I was looking for a way for the program to respond "good" to any sentence that would contain the word "fine" in it.
 
I'm sure it's something ridiculously easy, but I have no idea how to do it! :)
 Thank you,
    Miguel :)MSN Busca: fácil, rápido, direto ao ponto.  Encontre o que você quiser. Clique aqui. 

-- 
http://mail.python.org/mailman/listinfo/python-list