In this tutorial, I will explain how to use the strip() function in Python. As a Python developer, while working on a project for a New York client, I encountered a situation where I needed to use the split() function and explored various uses of the split() function. Let us learn more about this topic today.
Python strip() Function
The Python strip() function is a string method that removes specific characters from the beginning and end of a string. By default, it removes whitespace characters such as spaces, tabs, and newlines. However, you can also specify a set of characters to be removed.
The general syntax for the strip() function is as follows:
string.strip(characters)Here, string is the original string you want to modify, and characters is an optional argument that specifies the set of characters to be removed. If characters is not provided, the strip() function will remove whitespace characters by default.
Read How to Use the randint() Function in Python?
Remove Leading and Trailing Whitespace
One of the most common use cases for the strip() function is removing leading and trailing whitespace from a string. This is particularly useful when dealing with user input or data that may contain unwanted spaces. Let’s consider an example:
name = " John Doe "
print(name.strip()) Output:
John DoeI have executed the above code and added the screenshot below.

In this example, we have a string name that contains leading and trailing spaces. By calling the strip() function without any arguments, it removes the whitespace from both ends of the string, resulting in "John Doe".
Check out How to Use wait() Function in Python?
Remove Specific Characters
In addition to removing whitespace, the strip() function allows you to remove specific characters from the beginning and end of a string. This is handy when you need to clean up data or format strings in a specific way. Here’s an example:
phone_number = "+1-555-123-4567"
print(phone_number.strip("+-"))Output:
1-555-123-4567I have executed the above code and added the screenshot below.

In this case, we have a phone number string phone_number that includes a plus sign (+) at the beginning and a hyphen (-) at the end. By passing "+-" as the argument to the strip() function, it removes both the plus sign and the hyphen from the string, resulting in "1-555-123-4567".
Read Is Python a Good Language to Learn?
Example: Clean Up User Input
Let’s consider a real-world scenario where the strip() function can be extremely useful. Imagine you’re building a web application that requires users to enter their email addresses. However, users might accidentally include leading or trailing spaces when typing their email. This can lead to issues with validation and database storage.
email = " john.doe@example.com "
cleaned_email = email.strip()
print(cleaned_email)Output:
"john.doe@example.com"By using the strip() function, you can easily remove any leading or trailing whitespace from the user’s input, ensuring that the email address is stored correctly in your database.
Check out What Is the Best Way to Learn Python?
Combine strip() with Other String Methods in Python
The strip() function can be used in combination with other string methods to perform more advanced string manipulation. For example, you can use the split() method to split a string into a list of substrings based on a delimiter, and then apply the strip() function to each substring to remove unwanted characters.
address = "123 Main St., New York, NY 10001"
parts = address.split(",")
cleaned_parts = [part.strip() for part in parts]
print(cleaned_parts) # Output: ["123 Main St.", "New York", "NY 10001"]In this example, we split the address string into a list of substrings using the comma (,) as the delimiter. Then, we use a list comprehension to apply the strip() function to each substring, removing any leading or trailing whitespace. The resulting cleaned_parts list contains the individual components of the address without any unwanted spaces.
Check out How to Use the Python pop() Function?
Best Practices and Tips
When using the strip() function, keep the following best practices and tips in mind:
- Be cautious when specifying the characters to remove. Make sure to include all the characters you want to strip from the string.
- Keep in mind that the strip() function only removes characters from the beginning and end of the string. It does not remove characters from the middle of the string.
- If you only want to remove characters from one end of the string, you can use the lstrip() or rstrip() functions instead. These functions remove characters from the left or right side of the string, respectively.
- Remember that the strip() function returns a new string without modifying the original string. If you want to update the original string, you need to reassign the result back to the variable.
Read How to Use the ceil() Function in Python?
Conclusion
In this tutorial, I helped you to understand how to use the strip() function in Python. I discussed Python strip() function, how to remove leading and trailing whitespace, how to remove specific characters, real-time examples, combine strip() with other string methods in Python, and some best practices and tips.

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.