best way to replace first word in string?

Chris F.A. Johnson cfajohnson at gmail.com
Sat Oct 22 20:37:15 EDT 2005


On 2005-10-22, William Park wrote:
> Chris F.A. Johnson <cfajohnson at gmail.com> wrote:
>> On 2005-10-22, William Park wrote:
>> > hagai26 at gmail.com <hagai26 at gmail.com> wrote:
>> >> I am looking for the best and efficient way to replace the first word
>> >> in a str, like this:
>> >> "aa to become" -> "/aa/ to become"
>> >> I know I can use spilt and than join them
>> >> but I can also use regular expressions
>> >> and I sure there is a lot ways, but I need realy efficient one
>> >
>> > I doubt you'll find faster than Sed.
>> 
>>    On the contrary; to change a string, almost anything will be faster
>>    than sed (except another external program).
>> 
>>    If you are in a POSIX shell, parameter expansion will be a lot
>>    faster.
>> 
>>    In a python program, one of the solutions already posted will be
>>    much faster.
>
> Care to put a wager on your claim?

   In a shell, certainly.

   If one of the python solutions is not faster than sed (e.g.,
   os.system("sed .....")) I'll forget all about using python.

-- 
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>



More information about the Python-list mailing list