Lesson 4: Python .strip()

.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.

Function: .strip()

.strip() method removes leading and trailing whitespaces from a string. Either:

  • According to the character passed inside parenthesis as parameter or,
  • It will remove whitespaces if no argument is passed inside the parenthesis

Used Where?

  • Cleaning data
  • Removing specific leading and trailing characters from a string

Syntax do(s)

1) Type the characters you'd like to be wiped inside the parenthesis.

2) If no parameter is given .strip() removes whitespaces.

Syntax don't(s)

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.

Example 1: Strip example

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.

Example 2:

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.

Regex vs Python String Methods

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.

Tips

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.

Example 3: Cleaning multiple unwanted characters at once with strip method in Python

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)

Advanced Concepts

1- There are two more derivatives of the strip method for more targeted text parsing solutions. These are:
  • .rstrip() used for cleaning only right side of a string.
  • .lstrip() used for cleaning only left side of a string.
These string methods can be particularly useful when you want to only clean beginning or end of a string. Let’s see some examples.

Example 4: rstrip to clean right side only

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)

Example 5: lstrip to clean left side only

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)

A word on data cleaning methods such as strip method

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