Mastering String Manipulation with Python’s strip() Function

Mastering String Manipulation with Python’s strip() Function

4 mins read196 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Feb 28, 2023 08:15 IST

In this article, we will learn what strip function in python is, how to use strip functions with the help of examples. Later in the article, we will also learn some common error when using strip.

2023_02_MicrosoftTeams-image-192.jpg

Table of Content

Introduction to the strip function

Python’s strip() function removes any leading or trailing characters (whitespace or specific characters) from a given string. It returns a new string with the specified characters removed. The strip() function cleans up user input or manipulates strings in text processing applications. The function can also be customized by providing arguments to remove specific characters from the beginning or end of the string.

Syntax of the strip() function

 
string.strip([chars])
Copy code

The string parameter specifies the string to be stripped of leading or trailing characters, while the optional chars parameter specifies the characters to be removed from the string. The function will remove any whitespace characters by default if no chars parameter is provided. The strip() function returns a new string with the leading and trailing characters removed. Note that the strip() function does not modify the original string.

What is Programming What is Python
What is Data Science What is Machine Learning

Examples of using strip()

Removing leading and trailing whitespace

 
text = " Hello, World! "
clean_text = text.strip()
print(clean_text)
Copy code

Output

Removing specific characters from a string

 
text = ">>>Hello, World!<<<"
clean_text = text.strip("<>")
print(clean_text)
Copy code

Output

2023_02_image-177.jpg
Getting started with Python Strings
Getting started with Python Strings
Python strings are immutable, i.e., you cannot modify the contents of the object. In this article, we will briefly discuss how to create, assign string to a variable, how to...read more
Python Strings Practice Programs For Beginners
Python Strings Practice Programs For Beginners
Let us now look at some common Python practice programs based on strings that will help you master your foundations. I suggest you try to code...read more
String Formatting in Python
String Formatting in Python
In this article we discussed different techniques of string formatting in python like modulo operator, built-in format and f-string.

strip() function with arguments

The strip() function in Python can be customized by providing arguments to remove specific characters from the beginning or end of the string. The strip() function has two related methods: lstrip() and rstrip(), which remove characters from the left or right side of the string, respectively. Here are some examples:

Removing leading characters with lstrip()

 
# using lstrip()
text = "0000000Hello, World!"
clean_text = text.lstrip('0')
print(clean_text)
Copy code

Output

2023_02_image-178.jpg

In the above example, the lstrip() function removes all character ‘0 occurrences from the beginning of the string text.

Removing trailing characters with rstrip()

 
# using rstrip()
text = "Hello, World!0000000"
clean_text = text.rstrip('0')
print(clean_text)
Copy code

Output

In the above example, the rstrip() function removes all character ‘0 occurrences from the end of the string text.

Removing characters from both ends with strip()

 
text = "0000000Hello, World!0000000"
clean_text = text.strip('0')
print(clean_text)
Copy code

Output

In the above example, the strip() function removes all occurrences of the character ‘0’ from both the beginning and end of the string text.

How to Reverse a String in Python
How to Reverse a String in Python
In Python programming, strings are a sequence of characters or symbols. As a beginner practicing Python string operations, you will often come across problems that need you to work with...read more
String Template Class in Python
String Template Class in Python
In this article, we will learn about the template class of string module that is used to format the string in Python with the help of different examples.
How to Check if Two Python Strings are Anagrams
How to Check if Two Python Strings are Anagrams
An Anagram is a word or phrase that is formed by rearranging all the letters of a different word or phrase exactly once such as elbow and below. In this...read more

Common errors when using strip() function in Python

  • Not providing any arguments to strip(): If no arguments are provided to strip(), the function will remove any whitespace characters by default. However, if you want to remove specific characters, you must provide them as an argument.
  • Providing a non-string argument to strip(): The strip() function expects a string as its argument. If you provide a non-string argument, such as an integer or a list, the function will raise a TypeError.
  • Trying to modify the original string with a strip(): The strip() function returns a new string with the specified characters removed but does not modify the original string. If you want to modify the original string, you can assign the result of the strip() to the original variable.
  • Using strip() on a non-string object: The strip() function only applies to strings. If you try to apply it to a non-string object, such as an integer or a list, you will receive an AttributeError.
  • Forgetting to assign the result of the strip() to a variable: If you call to strip() without assigning the result to a variable, the stripped string will not be saved, and you will not be able to use it later in your code.
Programming Online Courses and Certification Python Online Courses and Certifications
Data Science Online Courses and Certifications Machine Learning Online Courses and Certifications

Conclusion

In conclusion, Python’s strip() function is a powerful tool for manipulating strings by removing leading or trailing characters. The function can be customized by providing arguments to remove specific characters from the string. Related methods like lstrip() and rstrip() can remove characters from the left or right side of the string, respectively.

Here are some key points to remember about the strip() function:

  • The strip() function removes leading and trailing characters from a string.
  • By default, strip() removes whitespace characters from a string, but you can provide an argument to remove specific characters instead.
  • The lstrip() method removes characters from the left side of a string, while the rstrip() method removes characters from the right side.
  • strip(), lstrip(), and rstrip() return a new string with the specified characters removed; they do not modify the original string.
  • Common errors when using strip() include not providing any arguments, providing a non-string argument, trying to modify the original string, using strip() on a non-string object, and forgetting to assign the result to a variable.
About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio