strip question

John Machin sjmachin at lexicon.net
Sat Jan 27 07:04:06 CET 2007



On Jan 27, 4:33 pm, eight02645... at yahoo.com wrote:
> hi
> can someone explain strip() for these :
> [code]>>> x='www.example.com'
> >>> x.strip('cmowz.')'example'
> [/code]

According to the documentation (with emphasis added):

"""Return a copy of the string with the *leading* and *trailing*
characters removed."""

Looking at the leading edge: "w" is in the set to strip, so it is
removed. Same applies to another 2 "w"s and a dot. "e" is not in the
strip set so it stops.
At the trailing edge: "m", "o", "c" and dot are removed, then it stops
because there's another "e".

> when i did this:
> [code]>>> x = 'abcd,words.words'
> >>> x.strip(',.')'abcd,words.words'
> [/code]
>
> it does not strip off "," and "." .Why is this so?

The comma and dot in your example are neither leading nor trailing.
The leading "a" is not in the set to strip, so nothing happens at the
front door.
The trailing "s" is not in the set to strip, so there's no back door
action either.

HTH,
John




More information about the Python-list mailing list