Python string Method - strip()

The strip() method is a built-in string method in Python that returns a copy of a string with leading and trailing whitespace characters removed.

The syntax for using strip() method is as follows:

r‮fe‬er to:theitroad.com
string.strip(characters)

Here, string is the original string, and characters is an optional parameter that specifies the set of characters to remove from the left and right sides of the string. If characters is not specified, the method removes all whitespace characters.

The method returns a new string with leading and trailing characters removed.

Here's an example of using the strip() method:

string = "   Hello, World!   "
trimmed_string = string.strip()
print(trimmed_string)

Output:

Hello, World!

In the example above, the strip() method was used to remove the leading and trailing whitespace characters from the original string " Hello, World! ". The resulting string is stored in the variable trimmed_string. Note that the leading and trailing whitespace characters are both removed. To remove only trailing or leading whitespace characters, you can use the rstrip() or lstrip() method, respectively.