nicer way to remove prefix of a string if it exists
Tim Chase
python.list at tim.thechases.com
Wed Jul 14 07:27:05 EDT 2010
On 07/13/2010 09:22 PM, Shashwat Anand wrote:
>> You could write:
>>
>> rsl = f[len(prefix):] if f.startswith(prefix) else f
>
> Or you can just do split and join, "".join(f.split(prefix, 1)) will do.
This suggestion breaks if the prefix occurs within the string
rather than at the beginning:
f = "this has file:// inside it"
prefix = "file://"
So your suggestion really just operates like
f.replace(prefix, '', 1)
-tkc
More information about the Python-list
mailing list