In python, the string function strip() gives a copy of the string with leading and trailing spaces removed. In this tutorial we’ll be looking its syntax and use-cases along with some examples.
Before we proceed, here’s a quick refresher on python strings – Strings are immutable sequences of unicode characters used to handle textual data. Strings implement all common sequence operations (example, iterations, membership tests, etc). For more, check out our guide on strings and other fundamentals of python.
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
The strip() function
As stated already,
strip() is a python string function used to remove leading and trailing spaces but it can be used to remove any given characters from the starting and end of the string. It is quite frequently used for text formatting purposes, for example, cleaning texts in forms filled out by users. The following is the syntax:
Here, sample_string is the string you want to truncate the leading and trailing chars from.
- chars (optional): String specifying the set of characters to be removed. If it’s not provided, it defaults to removing all the leading and trailing whitespace characters.
Returns: It returns a copy of the string with the leading and trailing chars removed.
Note: Strings are immutable and hence cannot be changed, string functions like
strip() return a copy of the string with its operations performed.
Example 1: Using the strip() function without any arguments
# strip() function to remove whitespace characters s1 = "Python strings are immutable" s2 = " Python strings are immutable " s3 = " \t\tPython strings are immutable \t\t" # using the strip() function on the above strings print(s1.strip()) print(s2.strip()) print(s3.strip())
Python strings are immutable Python strings are immutable Python strings are immutable
In the above example, the return values from applying the
strip() function on strings
s3 are the same because, by default, the
strip() function removes all the leading and trailing whitespace characters. Note that the tab space character
'\t' is also one of the whitespace characters in python.
Example 2: Using the strip() function to remove custom characters
# strip() function to remove custom characters s = "c co cod code gives bugs on running the code cod co c" # using the strip() function print(s.strip(' code'))
gives bugs on running th
In the above example, we pass the string
' code' as an argument to the
strip() function. This results in the function removing all instances of characters
'e' from the starting and end of the string
For more on string functions refer to the python docs.
Tutorials on python strings:
- Python – Trim Whitespace Characters from Strings
- Python – Reverse a String with Examples
- Python – Remove Character From String
- Python – Frequency of each word in String
- Python – Convert List to a String
- Python String Count – With Examples
- New String Functions in Python 3.9
- Python String Find – With Examples
- Python String Startswith – With Examples
- Python String Endswith – With Examples
- Python String Split – With Examples
- Python String Join – With Examples
- Python String Concatenation – With Examples
- Python String Replace – With Examples
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python String Strip – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.