In this Python tutorial, we will see what is strip function in Python String, its syntax, parameters, and different examples of how it will come in handy in Python. We will also see what is lstrip and rstrip in Python with examples.
Strip function in Python
In Python, the strip function is a built-in method that belongs to the string class. It is used to remove leading (at the beginning) and trailing (at the end) whitespace characters, such as spaces, tabs, and newline characters, from a Python string.
The strip function does not modify the original Python string but returns a new Python string with the leading and trailing whitespace removed.
Syntax and parameters
The syntax of the strip function Python is as follows:
- string: The original Python string from which leading and trailing whitespace will be removed.
- chars (optional): A Python string specifying the characters that need to be stripped. If not provided, the function will strip all whitespace characters.
Examples of strip() in Python
There are very different examples of using strip() in Python:
- Python strip() function remove whitespaces
- Python strip() function remove specific characters
- Python strip() function remove NewLine
let see them one by one:
Python strip function to remove whitespaces
Let’s say we have data of a state in US and there are unwanted spaces at the beginning and end of the Python strings:
state = " New York " print(state.strip())
The output will be: The leading and trailing spaces have been removed.
This way we can use the Python split function to remove whitespaces from the lead and trail of the string.
Python strip() method remove specific characters
Now, let’s say we have a Python string representing a US phone number, but it’s surrounded by unwanted characters that we want to remove:
phone = "(123)-456-7890!!!" print(phone.strip("(!)"))
The output is: The opening parentheses and exclamation marks have been removed from both ends, but the ones in the middle remain.
This way we can remove any specific character from a Python string using split function.
Python strip() function remove NewLine
Suppose, we have stored different states name of the US in a Python string:
usa_states = "\nAlabama, Alaska, Arizona, Arkansas, California, Colorado\n" print(usa_states) print(usa_states.strip('\n'))
The output is: For the first print the output will include two lines, one up and one down as we have given newline(\n) in the string leading and railing point. And after the strip method that two lines will be eliminated.
Alabama, Alaska, Arizona, Arkansas, California, Colorado Alabama, Alaska, Arizona, Arkansas, California, Colorado
This way we will use the strip function to remove newline from the string in Python.
Python lstrip and rstrip
As we can all see, the strip method is trimming the string from both side together. To trim the Python string only from one side of the string at a time, we can use:
Lets see them one by one.
The lstrip in Python
The lstrip() method in Python is used to remove leading characters (spaces or specified characters) from a string. “Leading” means the characters at the start of the string or the left side of the string.
For instance, Let’s say we are running a survey across the United States. We ask respondents about the state they live in, but some respondents prefix their answers with the country name ‘USA’ in the Python string, which we want to remove.
state_with_prefix = 'USA California' state = state_with_prefix.lstrip('USA ') print(state)
The output is: The Python lstrip() method removes the leading ‘USA ‘ from the string ‘USA California’, so we just get ‘California’.
This way we can use lstrip in Python to remove characters(substring or whitespaces) from the left of the string.
The rstrip in Python
The rstrip() method in Python is used to remove trailing characters (spaces or specified characters) from a string. “Trailing” means the characters at the end of the string or from the right side of the string.
For instance, Suppose we’re working with a dataset containing city names from across the US, but some of the entries have state abbreviations appended at the end in Python string that we need to remove.
city_with_suffix = 'Los Angeles CA' city = city_with_suffix.rstrip(' CA') print(city)
The output is: The Python rstrip() method removes the trailing ‘ CA’ from the string ‘Los Angeles CA’, so we just get ‘Los Angeles’.
This way we can use rstrip in Python to remove characters(substring or whitespaces) from the right of the string.
Note: The strip function in string can only remove the characters either from left or right or both but can never remove characters from the middle of the Python string.
The strip() function in Python is a powerful tool to clean and sanitize text data by removing leading and trailing whitespace characters. We’ve demonstrated how to use the strip() function with examples, We have also seen what is lstrip and rstrip in Python with examples.
You may also like to read:
- Remove substring from string in Python if exist
- How to remove the last character from a string in Python
- How to get string values from list in Python
- Lower function in Python string
- Count function in Python string
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.