Python String strip() Method – Tutorial with Examples

Python String strip() Method

The strip() method is a built-in method in Python that returns a string with all the leading and trailing whitespace removed. It is a useful method for cleaning up user input or manipulating text data.

Syntax

The syntax of the strip() method is as follows:

string.strip([chars])

Here, string is the string that we want to strip the whitespace from, and chars is an optional argument that specifies a set of characters to remove from the beginning and end of the string. If this argument is not specified, then all whitespace characters (spaces, tabs, and newlines) are removed.

Return Value

The strip() method returns a new string with all the leading and trailing whitespace removed.

Examples

Here are three different examples of how to use the strip() method in Python:

Example 1: Stripping Whitespace from a String

The following example demonstrates how to use the strip() method to remove all whitespace characters from a string:

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

Output:

Hello, World!

In this example, we define a string that contains leading and trailing whitespace and use the strip() method to remove the whitespace characters. The resulting string is ‘Hello, World!’.

Example 2: Stripping Specific Characters from a String

The following example demonstrates how to use the strip() method to remove specific characters from a string:

string = ":-)Hello, World!:-)"
stripped_string = string.strip(':-)')
print(stripped_string)

Output:

Hello, World!

In this example, we define a string that contains leading and trailing smiley faces and use the strip() method to remove the smiley faces. The resulting string is ‘Hello, World!’.

Example 3: Stripping Whitespace from a Multiline String

The following example demonstrates how to use the strip() method to remove leading and trailing whitespace from a multiline string:

string = "   Hello, World!\n\n   "
stripped_string = string.strip()
print(stripped_string)

Output:

Hello, World!

In this example, we define a multiline string that contains leading and trailing whitespace and use the strip() method to remove the whitespace characters. The resulting string is ‘Hello, World!’. Notice that the trailing newline character is still present in the resulting string.

Use Cases

The strip() method can be used in a variety of situations where whitespace or other characters need to be removed from the beginning or end of a string. Some common use cases include:

  • Cleaning up user input by removing leading and trailing whitespace
  • Extracting relevant information from unstructured text data by removing unwanted characters
  • Preparing data for analysis by removing unnecessary characters or formatting

Overall, the strip() method in Python is a useful tool for manipulating and cleaning up text data. It can remove both leading and trailing whitespace as well as specific characters from a string. The method can be used in various use cases such as cleaning up user input, extracting relevant information from text data, and preparing data for analysis.

Leave a Reply

Your email address will not be published. Required fields are marked *