Python trim strings with strip method [with Examples]

The strip method is used to trim specified leading and trailing characters from Python strings . To understand better, consider the following strings:

 A = "   How are you?   "
 B = "***How are you?***"
 C = ",*& &&How are you?*+&,,,"

In all 3 cases, let’s assume that we have the task of extracting out the phrase : “How are you ?”. To do that we need to trim the strings of the leading and trailing characters and spaces. The strip function can help us get that done.

Here’s how the logic works:

  • In case of variable “A”, there is only leading and trailing white space. We would need to use the strip method to remove this.
  • In case of variable “B”, the “*” character leads and trails the target string. This is going to be trimmed by the strip function.
  • In case of variable “C”, it’s slightly more complex. We have all the following characters : ,*&+ apart from some amount of space. Can this be trimmed with strip method?

The Python strip function syntax

The strip function syntax is as follows:

string.strip("ENTER_CHARS_TO_STRIP_HERE")

So understand better, let’s apply it on the strings A, B & C to extract out “How are you?”:

#we can trim blank space by passing nothing to the strip function
 
print(A.strip())

#In second case we need to pass * wrapped within quotes 

print(B.strip("*"))

#In 3rd case we need to pass all the characters including blank to the strip function 

print(C.strip(",*&+ "))

When we run the above code, here’s what we get:

How are you?
How are you?
How are you?

Now consider the following line of code. It’s same as above, we just dropped the additional space. In other words, instead of “,*&+ ” , we have “,*&+”

print(C.strip(",*&+"))

Here’s what the output looks like:

 &&How are you?

It’s easy to explain what just happened and it reveals a very important detail about how the strip function works.

It starts checking for characters to delete from the leading part of the string. As soon as it encounters a character that has not been fed, it stops trimming the leading part and jumps to the trailing part of the string.

In this case, it encounter space between the “&”s. Since it’s not supposed to trim space as per the code – it jumped to the right end of the string. As a result the 2 “&”s right before the target string survived.

lstrip() function in Python

If we want to remove characters only from the leading part (or the left part of a string, we would use the lstrip() function.

The syntax of the lstrip() function is as follows:

string.lstrip("ENTER_CHARS_TO_STRIP_HERE")

To understand better let’s consider the following example:

string = "***How are you?***"
string.lstrip("*")

The output in the above case would be :

How are you?***

rstrip() function in Python

If we want to remove characters only from the trailing part (or the right part of a string, we would use the rstrip() function.

he syntax of the rstrip() function is as follows:

 string.rstrip("ENTER_CHARS_TO_STRIP_HERE") 

To understand better let’s consider the following example:

string = "***How are you?***"
string.rstrip("*")

The output in the above case would be :

*** How are you?

Summary of strip , lstrip & rstrip function:

Python trim string with strip , lstrip & rstrip functions
Python trim string with strip , lstrip & rstrip functions

When to use the strip function to trim string in Python

One of the key steps , in Python based text handling is to clean the text. Especially, leading and trailing spaces are a big menace since they are not easily detectable. It thus makes sense to trim these right at the beginning using the strip function.

Do note that strip function is not to be used when you need to trim characters from in between a text. For example, if you want to clean punctuation s from a text you ideally should make use of the replace method in Python strings or use the method to replace regex in Python strings.

FAQs : Python trim string

What is the difference between strip , lstrip and rstrip function in Python ?

Strip, lstrip & rstrip functions are used to trim specificed characters from Python strings. While strip function trims characters from both ends of the string, lstrip trims python strings only from the left end and rstrip trims python strings only from the right end.

How do you ignore spaces in Python?

You can trim leading and trailing white spaces from Python strings by using the strip method.

Summary
Python trim strings with strip method [with Examples]
Article Name
Python trim strings with strip method [with Examples]
Description
Wondering how to trim leading and trailing characters from a Python string ? Strip method is what you are looking for. Read on to learn more.
Author
Publisher Name
Digital Marketing Chef
Publisher Logo

3 thoughts on “Python trim strings with strip method [with Examples]”

  1. Hello are using WordPress for your blog platform? I’m new to the
    blog world but I’m trying to get started and
    create my own. Do you need any html coding expertise to make your own blog?

    Any help would be really appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *