Weird behavior of strip()

Question:

>>> adf = "123 ABCD#"
>>> df = "<ABCD#>" 
>>> adf.strip(df)
>>> '123 '
>>> xc = "dfdfd ABCD#!"
>>> xc.strip(df)
>>> 'dfdfd ABCD#!'

Why does strip() take out ABCD# in adf?
Does strip completely ignore “<” and “>” ?Why does it remove the chars when no “<” and “>” are there in the original string?

Asked By: typing…

||

Answers:

The method strip() returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters).

The characters that are in df, they occur at the end in the string adf. This is not the case in string xc where the first and last character are ! and d.

str.strip([chars]); => If any character in str occurs in chars at last or first index, then that character is stripped off from str. Then it again checks. When no character is stripped, it stops.

Answered By: Raza
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.