<div><br></div><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><blockquote type="cite"><div dir="auto"><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_quote">And this really is simple enough that I don't want to reach for regex's for it. That is, I'd write it by hand rather than mess with that.</div></div></blockquote></div></div><div dir="auto"><br></div><div dir="auto">Well, with re.escape it's not messy at all :</div></div></blockquote></div></div></blockquote><div dir="auto"><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><blockquote type="cite"><div dir="auto"><div dir="auto"></div><div dir="auto">You could do a ltrim function in one line :</div><div dir="auto"><br></div><div dir="auto">def ltrim(s, x):</div><div dir="auto">    return re.sub("^" + re.escape(x), '', s)</div></div></blockquote></div><div>I think</div><div><br></div><div>    re.sub("^" + re.escape(x), '', s)</div><div><br></div><div>is a lot more messy and hard to read than</div><div><br></div><div>    s[len(prefix):] if s.startswith(prefix) else s </div><div><br></div><div>it's also roughly an order of magnitude slower.</div></div></blockquote><div dir="auto"><br></div><div dir="auto">I agree, but I said I wouldn’t choose to “mess” with regex, not that the resulting code would be messy. </div><div dir="auto"><br></div><div dir="auto">There is a substantial cognitive load in working with regex—they are another language.  If you aren’t familiar with them (I’m not) it would take some time, and googling, to find that solution. </div><div dir="auto"><br></div><div dir="auto">If you are familiar with them, you still need to import another module and end up with an arguably less readable and slower solution.</div><div dir="auto"><br></div><div dir="auto">Python was designed from the beginning not  to rely on regex for simple string processing, opting for fairly full featured set of string methods. These two simple methods fit well into that approach.</div><div dir="auto"><br></div><div dir="auto">-CHB</div><div dir="auto"><br></div><div dir="auto"><br></div></div></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">Christopher Barker, PhD<br><br> Python Language Consulting<br>  - Teaching<br>  - Scientific Software Development<br>  - Desktop GUI and Web Development<br>  - wxPython, numpy, scipy, Cython<br></div>