A few things you might find useful:<br><div class="im"><br>1) Strings can be accessed like arrays: string_1[3] == 'b'<br>
<br>
2) Strings can be iterated over:<br>
<br>
for ch in s:<br>
do something with ch<br>
<br></div>
3) Strings have a "find" method. They also have a "startswith" method.<div class="im"><br>
4) For more advanced searching there is the regular expression library<br>
in the "re" module of the standard library.<br>
<br></div>5) You might find the "zip" built-in useful for iterating over two things at once:<br><div class="im"><br><span style="font-family: courier new,monospace;">match = []</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">for c1,c2 in zip(string_1, string_2):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if c1 == c2:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> match.append(c1)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> else:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> break</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">match = "".join(match)<br><br></span></div>
6) I highly recommend David Beazley's book, "Python Essential Reference,"<div class="im"><br>
which has all of this and much, much more.<br>
</div><font color="#888888"><font color="#888888"><br>
Chris<br><br></font></font><br><br><div class="gmail_quote">On Thu, Apr 2, 2009 at 9:15 AM, Joseph Mack NA3T <span dir="ltr"><<a href="mailto:jmack@wm7d.net">jmack@wm7d.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I've looked in the string methods/functions in the python docs and I can't see how to do what I want, which is to find the parts of strings that match. eg<br>
<br>
<br>
string_1 = "foobar"<br>
string_2 = "foobaz"<br>
<br>
matched_string = "fooba"<br>
<br>
I need to walk along the string(s) 1 char at a time, accepting matching letters, till I get a mismatch, when the code exits. I was expecting to be able to retrieve chars one at a time from each of the two strings and test if the chars were the same.<br>
<br>
How do I do this in python?<br>
<br>
Thanks Joe<br>
<br>
-- <br>
Joseph Mack NA3T EME(B,D), FM05lw North Carolina<br>
jmack (at) wm7d (dot) net - azimuthal equidistant map<br>
generator at <a href="http://www.wm7d.net/azproj.shtml" target="_blank">http://www.wm7d.net/azproj.shtml</a><br>
Homepage <a href="http://www.austintek.com/" target="_blank">http://www.austintek.com/</a> It's GNU/Linux!<br>
<br>
_______________________________________________<br>
triangle-zpug mailing list<br>
<a href="mailto:triangle-zpug@starship.python.net" target="_blank">triangle-zpug@starship.python.net</a><br>
<a href="http://starship.python.net/mailman/listinfo/triangle-zpug" target="_blank">http://starship.python.net/mailman/listinfo/triangle-zpug</a><br>
</blockquote></div><br>