[Tutor] find

john public apython101@yahoo.com
Tue, 4 Dec 2001 18:09:18 -0800 (PST)


--0-1855971381-1007518158=:47978
Content-Type: text/plain; charset=us-ascii




John, 

>t2 
>a little red moon 
>Traceback (most recent call last): 
> File "C:\Python21\Tools\idle\amembership.py", line 
>10, in ? 
> if ("re" in t2): 
>TypeError: 'in <string>' requires character as left 
>operand 

As the error message states, the thing for which you are asking "in" to 
look for in some string must be a character, or equivalently a string 
of 
lenght one. So, you can ask: "is letter 'r' in this string?" but not 
"is 
word 're' in this string?" Of course, Python has ways to look for words 
in 
strings as well. Check out the string module here: 
http://www.python.org/doc/current/lib/module-string.html and in 
particular 
look at string.find() and (perhaps) string.count(). 


ok so the membership function only looks for single charectors. So I was using the wrong tool for the job.

after looking at the Python library reference 4.1 I took a newbie guess and thought that:

find(s, sub[, start[,end]]) 
   Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices. 
was the right tool for the job.

I interpreted and applied as below:


t1 = "a little red moon"
t2 = "cute redhead"
t3 = "redmond washington"

if ("r" in t2):
    print "t2"
    print t2

find(t2,red[0[1]])  

and got:

Python 2.1.1 (#20, Jul 20 2001, 01:19:29) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> 
t2
cute redhead
Traceback (most recent call last):
  File "C:\Python21\Tools\idle\amembership.py", line 10, in ?
    find(t2,red[0[1]])
NameError: name 'find' is not defined


am I barking up the right tree?

thanks 



---------------------------------
Do You Yahoo!?
Buy the perfect holiday gifts at Yahoo! Shopping.
--0-1855971381-1007518158=:47978
Content-Type: text/html; charset=us-ascii

<P><BR><BR><FONT color=darkred><EM>John, <BR><BR>&gt;t2 <BR>&gt;a little red moon <BR>&gt;Traceback (most recent call last): <BR>&gt; File "C:\Python21\Tools\idle\amembership.py", line <BR>&gt;10, in ? <BR>&gt; if ("re" in t2): <BR>&gt;TypeError: 'in &lt;string&gt;' requires character as left <BR>&gt;operand <BR><BR>As the error message states, the thing for which you are asking "in" to <BR>look for in some string must be a character, or equivalently a string <BR>of <BR>lenght one. So, you can ask: "is letter 'r' in this string?" but not <BR>"is <BR>word 're' in this string?" Of course, Python has ways to look for words <BR>in <BR>strings as well. Check out the string module here: <BR>http://www.python.org/doc/current/lib/module-string.html and in <BR>particular <BR>look at string.find() and (perhaps) string.count(). <BR><BR><BR></EM></FONT><STRONG><FONT color=black>ok so the membership function only looks for single charectors. So I was using the wrong tool for the job.</FONT></STRONG></P>
<P><STRONG>after&nbsp;looking at the Python library reference 4.1 I took a newbie guess and thought that:</STRONG></P>
<DL>
<DT><B><A name=l2h-632><TT class=function>find</TT></A></B>(<VAR>s, sub</VAR><BIG>[</BIG><VAR>, start</VAR><BIG>[</BIG><VAR>,end</VAR><BIG>]</BIG><BIG>]</BIG>) 
<DD>Return the lowest index in <VAR>s</VAR> where the substring <VAR>sub</VAR> is found such that <VAR>sub</VAR> is wholly contained in <CODE><VAR>s</VAR>[<VAR>start</VAR>:<VAR>end</VAR>]</CODE>. Return <CODE>-1</CODE> on failure. Defaults for <VAR>start</VAR> and <VAR>end</VAR> and interpretation of negative values is the same as for slices. </DD></DL>
<P><STRONG>was the right tool for the job.</STRONG></P>
<P><STRONG>I interpreted and applied as below:</STRONG></P>
<P><BR>t1 = "a little red moon"<BR>t2 = "cute redhead"<BR>t3 = "redmond washington"</P>
<P>if ("r" in t2):<BR>&nbsp;&nbsp;&nbsp; print "t2"<BR>&nbsp;&nbsp;&nbsp; print t2</P>
<P>find(t2,red[0[1]])&nbsp; </P>
<P><STRONG>and got:</STRONG></P>
<P>Python 2.1.1 (#20, Jul 20 2001, 01:19:29) [MSC 32 bit (Intel)] on win32<BR>Type "copyright", "credits" or "license" for more information.<BR>IDLE 0.8 -- press F1 for help<BR>&gt;&gt;&gt; <BR>t2<BR>cute redhead<BR>Traceback (most recent call last):<BR>&nbsp; File "C:\Python21\Tools\idle\amembership.py", line 10, in ?<BR>&nbsp;&nbsp;&nbsp; find(t2,red[0[1]])<BR>NameError: name 'find' is not defined<BR></P>
<P><STRONG>am I barking up the right tree?</STRONG></P>
<P>thanks </P><p><br><hr size=1><b>Do You Yahoo!?</b><br>
Buy the perfect holiday gifts at <a
href="http://rd.yahoo.com/O=1/I=brandr-mailfooter/*http://shopping.yahoo.com">Yahoo! Shopping</a>.
--0-1855971381-1007518158=:47978--