What Is “strip” in Python?

In Python, “strip” is a method that eliminates specific characters from the beginning and the end of a string. By default, it removes any white space characters, such as spaces, tabs and new line characters.

The common syntax for this command appends it to the name of a string variable (str) as such: “str.strip()”. For example, the command “‘ spacious ‘.strip()” removes all of the spaces at the beginning and the end of the string. The output is “‘spacious'”. The string variable can also be passed as an argument, along with the characters that one wants to remove. In this case, the syntax is: “string.strip(str[, chars])”. Here “str” is the string that is stripped, and “chars” is an optional string that contains the characters which need to be removed.

ADVERTISEMENT