strip module bug

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Mon Oct 13 09:11:28 EDT 2008


Poppy:
> 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'

Python V.2.5 is not flawless, but you can't find bugs so easily. I've
found only two bugs in about three years of continuous usage (while I
have found about 27 new different bugs in the DMD compiler in about
one year of usage).

str.strip() isn't a module, and generally in Python it's called
"method", in this case a method of str (or unicode).

str.strip() as optional argument doesn't take the string you want to
remove, but a string that represent the set of chars you want to strip
away from both ends, so:

>>> "detail.xml".strip("lmx.")
'detai'
>>> "detail.xml".strip("abcdlz")
'etail.xm'

To strip the ending ".xml" you can use the str.partition() method, or
an alternative solution:

>>> var = "detail.xml"
>>> suffix = ".xml"
>>> if var.endswith(suffix):
...     var2 = var[: -len(suffix)]
...
>>> var2
'detail'

Bye,
bearophile



More information about the Python-list mailing list