Issue 6809: Python string.lstrip bug? (original) (raw)

A simple lstrip on the following causes an extra character to be stripped, as per the below. Tried on 2.6.1 and on 2.4.3, as below.

Python 2.6.1 (r261:67515, Feb 27 2009, 02:54:13) [GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2 Type "help", "copyright", "credits" or "license" for more information.

a = "contact_work_phone_no" a.lstrip("contact") 'work_phone_no' a.lstrip("contact") 'work_phone_no' a = "contact_city" a.lstrip("contact_") 'ity' a.lstrip("con") 'tact_city' a.lstrip("contact") 'city' a.lstrip("contact") 'ity'

Python 2.4.3 (#1, Mar 14 2007, 19:01:42) [GCC 4.1.1 20070105 (Red Hat 4.1.1-52)] on linux2 Type "help", "copyright", "credits" or "license" for more information.

a = "contact_city" a.lstrip("contact_") 'ity'

This is not a bug: the argument to lstrip effectively specifies a set of characters to be removed; in your example, 'c' is in that set, so the 'c' at the beginning of city gets removed. 'i' is not in that set, so it stays.

lstrip(...) S.lstrip([chars]) -> string or unicode

Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.