.strip()
method is a convenient string method to clean whitespaces or other characters from the beginning and end of a string. It can be very handy when working with raw data, files, email data, multiples lines etc. .strip()
method is a convenient string method to clean whitespaces or other characters from the beginning and end of a string. It can be very handy when working with raw data, files, email data, multiples lines etc. .strip() method removes leading and trailing whitespaces from a string. Either:
1) Type the characters you'd like to be wiped inside the parenthesis.
2) If no parameter is given .strip() removes whitespaces.
1) Since strip is a string function it can only be applied to Python strings. However, you can convert a number to string and try to apply strip method to it that way if you really must.
Here are some of the most famous women athletes in skiing and their international alpine ski competition medal counts. Unfortunately we have the values in separate Python list but thankfully they are in exact right order.
name = " Lesson Begins "
>>> a = name.strip()
>>> print(a)
“Lesson Begins”
strip method intelligently identifies empty spaces on both end. Also notice that, when we pass no argument to the strip method it automatically defaults to cleaning unnecessary whitespaces.
What about some characters? Let’s say we’re downloading a stream of data and unfortunately there are some unwanted characters encoded everywhere.
name = "###Lesson Begins"
a = name.strip("#")
print(a)
“Lesson Begins”
Imagine if this was a nation wide data and there was so much critical insight that can be gained from it. Parsing it manually would take years or decades.
If you are interested in advanced text analysis you should definitely check out Regex, short for Regular Expressions. It’s extremely powerful for textual analysis and manipulation and once you get used to it, it will definitely step up your programming skills.
Regex is used in database queries, search engines, data collection for data science, web parsing, dynamic games with textual content, financial data analysis, business analysis and technically everywhere there is a complex textual operation.
Recommended Read:
Regex is incorporated in many programming languages including of course Python.
String Methods (such as the one we are currently working on, strip) on the other hand are very practical solutions to specific string manipulations that apply to Python strings.
1- You don’t have to type the characters you want to remove in a specific order. You can just throw them all together and .strip() method will remove all of them on both sides.
Let’s see some examples.
We can make a cocktail of string from unwanted characters and strip method will apply them all to clean our Python string.
name = "#$%%% ### @Lesson Begins"
a = name.strip("#@%$ ")
print(a)
“Lesson Begins”
This approach can particularly be helpful when there is more cleaning to do than just whitespace (empty strings)
.rstrip()
used for cleaning only right side of a string..lstrip()
used for cleaning only left side of a string.We can make a cocktail of string from unwanted characters and strip method will apply them all to clean our Python string.
name = "1. Lesson Begins:.........................."
a = name.rstrip(".")
print(a)
“1. Lesson Begins:”
This approach can particularly be helpful when there is more cleaning to do than just whitespace (empty strings)
We can make a cocktail of string from unwanted characters and strip method will apply them all to clean our Python string.
name = " @@@:::Lesson Begins:"
a = name.lstrip("@: ")
print(a)
“Lesson Begins:”
This approach can particularly be helpful when there is more cleaning to do than just whitespace (empty strings)
In this lesson we have covered a simple yet powerful Python string method: strip. Even if you forgot the name of the exact method it would be very beneficial to be aware of this method’s existence and how to apply it when necessary.
When doing data science or data analysis or any task that involves data, data often comes in different formats and shapes than we expect. If you don’t know how to parse and clean data properly this shortcoming can use up your time and intellectual resources that you would rather focus on analysis and bring you anxiety.
Being fluent in such methods (and many more including regex) can mean feeling confident, reaching flow state and achieving sustainable success in your programming or data projects.
In next lesson, we will take a look at another Python concept that brings programmers confidence: dir function.
Next Lesson: dir() function