The strip()
method is a built-in Python function that removes unwanted leading and trailing characters from a string. The characters that are removed depend on the argument that is passed to the strip()
method. By default, the strip()
method removes whitespace characters such as spaces, tabs, and newlines.
Removing Whitespaces from a String
To remove leading and trailing whitespaces from a string, you can simply call the strip()
method on the string. Here’s an example:
text = " Hello, World! "
print(text.strip()) # Output: "Hello, World!"
In this example, we have defined a string text
that contains leading and trailing whitespaces. We then call the strip()
method on the string to remove the whitespaces.
Removing Specific Characters from a String
You can also use the strip()
method to remove specific characters from a string. To do this, you need to pass a string argument to the strip()
method that contains the characters you want to remove. Here’s an example:
text = "Hello, World!!!"
print(text.strip("!")) # Output: "Hello, World"
In this example, we have defined a string text
that contains exclamation marks at the end. We then call the strip()
method on the string and pass the exclamation mark character as the argument to remove it from the end of the string.
Removing Multiple Specific Characters from a String
You can also remove multiple specific characters from a string by passing a string argument to the strip()
method that contains all the characters you want to remove. Here’s an example:
text = "Hello, World!!!"
print(text.strip("!l")) # Output: "Hello, World"
In this example, we have defined a string text
that contains exclamation marks and the letter ‘l’ at the end. We then call the strip()
method on the string and pass the characters ‘!l’ as the argument to remove them from the end of the string.
Removing Whitespaces from a Specific Side of a String
Sometimes, you may want to remove whitespaces from only the left or right side of a string. You can achieve this by using the lstrip()
or rstrip()
methods, respectively. Here are some examples:
text = " Hello, World! "
print(text.lstrip()) # Output: "Hello, World! "
print(text.rstrip()) # Output: " Hello, World!"
In the first example, we call the lstrip()
method on the string text
to remove the leading whitespaces. In the second example, we call the rstrip()
method on the string text
to remove the trailing whitespaces.
Removing Whitespaces from a Specific Side of a String with Specific Characters
You can also use the lstrip()
and rstrip()
methods to remove specific characters from a specific side of a string. Here’s an example:
text = "Hello, World!!!"
print(text.rstrip("!")) # Output: "Hello, World"
In this example, we have defined a string text
that contains exclamation marks at the end. We then call the rstrip()
method on the string and pass the exclamation mark character as the argument to remove it from the end of the string.
Conclusion
The strip()
method is a powerful tool that can help you remove unwanted characters from a string in Python. By default, it removes whitespace characters, but you can also use it to remove specific characters from a string. Additionally, you can use the lstrip()
and rstrip()
methods to remove characters from only one side of a string.