strip module bug
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Mon Oct 13 09:29:32 EDT 2008
On Mon, 13 Oct 2008 08:50:41 -0400, Poppy wrote:
> I'm using versions 2.5.2 and 2.5.1 of python and have encountered a
> potential bug. Not sure if I'm misunderstanding the usage of the strip
> function but here's my example.
>
> var = "detail.xml"
> print var.strip(".xml") ### expect to see 'detail', but get 'detai'
> var = "overview.xml"
> print var.strip(".xml") ### expect and get 'overview'
I got bitten by this once too. Most embarrassingly, I already knew the
right behaviour but when somebody suggested it was a bug I got confused
and convinced myself it was a bug. It's not.
You have misunderstood what strip() does. It does NOT mean "remove this
string from the string if it is a suffix or prefix".
Consider:
>>> "abcd123".strip('123')
'abc'
>>> "abcd123".strip('321')
'abc'
>>> "abcd123111".strip('213')
'abc'
strip() removes *characters*, not substrings. It doesn't matter what
order it sees them.
See help(''.strip) in the interactive interpreter for more detail.
By the way, the right way to deal with file extensions is:
>>> import os
>>> os.path.splitext('detail.xml')
('detail', '.xml')
--
Steven
More information about the Python-list
mailing list