best way to replace first word in string?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Oct 22 20:36:29 EDT 2005


On Sat, 22 Oct 2005 21:41:58 +0000, Ron Adam wrote:

> Don't forget a string can be sliced.  In this case testing before you 
> leap is a win.   ;-)

Not much of a win: only a factor of two, and unlikely to hold in all
cases. Imagine trying it on *really long* strings with the first space
close to the far end: the split-and-join algorithm has to walk the string
once, while your test-then-index algorithm has to walk it twice.

So for a mere factor of two benefit on short strings, I'd vote for the
less complex split-and-join version, although it is just a matter of
personal preference.

-- 
Steven.




More information about the Python-list mailing list