Python strip() function Explained [Easy Examples]

 

Introduction to Python strip() function

Sometimes we need to remove the first or last or both first and last characters from the string when we work with string data. Or we may don't want the whitespaces or some specific characters and want to remove those all from a string. The python strip method helps us to remove characters from a string. In this tutorial, we will learn about the python strip method, its syntax, and some examples. Also, we will see the difference between lstrip and rstrip using various examples. Moreover, we will solve different examples of trimming special characters, whitespaces, and specified elements from a string using the python strip method. All in all, this tutorial contains all the information that you need to know about the python strip method.

 

Getting started with the python strip() function

String manipulation in Python is achieved through a set of built-in methods, which return new strings since they're immutable. One of those built-in methods is the python strip method which is used to trim/remove characters from a string. In this section, we will discuss about the syntax of the python strip method and will solve some real examples.

Advertisement

 

Syntax and the return value of Python strip method

The python strip method is a built-in function in python that is used to remove specific characters from a string. Here is a simple syntax of the python strip method.

string_name.strip("character")

The python strip method has an optional parameter. When this parameter is not provided, the method will remove space from the starting and ending of string data. But if you want to remove the specific character from the starting and end of the string then you have to set the character as an argument value for the method. It returns the main string value after removing the particular characters from the string.

The return value of the python strip is also string type. We can confirm it by using the python type method. See the example below:

# string
string = "bashir alam"

# printing the type of python strip
print(type(string.strip()))

Output:

<class 'str'>

Notice that the return type is also string.

 

Difference between lstrip and rstrip methods

The lstrip and rstrip are also in a sub-category of the python strip method. The following is the syntax of python lstrip and python rstrip methods.

Advertisement
# Python lstrip syntax
string_name.lstrip()

# python rstrip syntax
string_name.lstrip()

lstrip() : The 'l' in the method's name corresponds to left, and this method strips white spaces or characters mentioned to the left of the input string.

rstrip() : The 'r' in the method's name correspond to right, and this method  strips white spaces or characters mentioned to  the right of the input string

 

Examples of stripping white spaces from a string

Now let us take real examples and use the python strip method in different situations.

 

Example-1: Use python strip() to trim any string

Here we will take a string with unnecessary whitespaces in the beginning and at the end and will use python strip to remove those whitespaces. See the following example.

# declare string
string = "      bashir alam      "
result ="|{}|"

# printing unstripped string
print("Untripped string:",result.format(string))

# printing the strip string using python strip
print("stripped string:", result.format(string.strip()))

Output:

Unstripped string: |       bashir alam       |
Stripped string: |bashir alam|

Notice that when we didn't provide any argument, then by default the python strip method removes the whitespaces from starting and ending of the string. You can see this, in the above example as well.

Now we may not want to remove the whitespaces from both sides. Let us say we want to remove the whitespaces from the only left side or only right side. Here comes python lstrip and python rstrip which only removes the characters from the left and right side respectively.

 

Example-2: Use python lstrip() to trim left side of the string

See the example below, which only removes the whitespaces from the left side.

Advertisement
# string
string = "      bashir alam        "
result ="|{}|"

# printing unstripped string
print("Unstripped string:",result.format(string))

# printing the stripped string using python lstrip
print("Stripped string:", result.format(string.lstrip()))

Output:

Unstripped string: |       bashir alam       |
Stripped string: |bashir alam       |

Notice that this time we successfully removed the whitespaces from the only left side.

 

Example-3: Use python rstrip() to trim right side of the string

Now let us remove only the right whitespaces. See the example below:

# string
string = "      bashir alam        "
result ="|{}|"

# printing unstripped string
print("Unstripped string:",result.format(string))

# printing the stripped string using python rstrip
print("Stripped string:", result.format(string.rstrip()))

Output:

Unstripped string: |       bashir alam       |
Stripped string: |       bashir alam|

 

Examples of stripping specific characters from a string

Now let us say we want to remove specific characters from a string. We can do that by using the python strip method with passing an argument. Let us say we want to remove b from the string starting of the string. See the example below:

# string
string = "bashir alam"
result ="|{}|"

# printing unstripped string
print("Unstripped string:",result.format(string))

# printing the stripped string using python strip
print("Stripped string:", result.format(string.strip("b")))

Output:

Unstripped string: |bashir alam|
Stripped string: |ashir alam|

Notice that we have removed character b from the string successfully. We can also use lstrip to remove the character from the left side. See the example below:

# string
string = "bashir alam"
result ="|{}|"

# printing unstripped string
print("Unstripped string:",result.format(string))

# printing the stripped string using python lstrip
print("Stripped string:", result.format(string.lstrip("b")))

Output:

Advertisement
Unstripped string: |bashir alam|
Stripped string: |ashir alam|

We got the same result; b character has been removed from the left side. In a similar way, we can remove specified characters from the end of a string using rstrip method. See the following example.

# string
string = "bashir alam"
result ="|{}|"

# printing unstripped string
print("Unstripped string:",result.format(string))

# printing the stripped string using python rstrip
print("Stripped string:", result.format(string.rstrip("m")))

Output:

Unstripped string: |bashir alam|
Stripped string: |bashir ala|

 

Examples of stripping multiple characters from a string

Using the same methods, we can remove multiple characters as well. Let us now remove the first name from the string using the python strip method.

# string
string = "bashir alam"
result ="|{}|"

# printing unstripped string
print("Unstripped string:",result.format(string))

# printing the stripped string using python strip
print("Stripped string:", result.format(string.strip("bashir")))

Output:

Unstripped string: |bashir alam|
Stripped string: | alam|

Actually, the python strip method removes the matched string from both sides. In our previous example, there was no matched string at the end, that is why it was only removed from the starting. Now let us remove from the end and starting of string. See the example below:

# string
string = "bashir alam bashir"
result ="|{}|"

# printing unstripped string
print("Unstripped string:",result.format(string))

# printing the stripped string using python strip
print("Stripped string:", result.format(string.strip("bashir")))

Output:

Unstripped string: |bashir alam bashir|
Stripped string: | alam |

Notice that this time we were able to successfully removed the specified string from the end and starting. We can also apply lstrip and rstrip methods to remove strings from starting and ending as well. See the following example.

# string
string = "bashir alam"
result ="|{}|"

# printing unstripped string
print("Unstripped string:",result.format(string))

# printing the stripped string using python lstrip
print("l-tripped string:", result.format(string.lstrip("bashir")))

# printing the stripped string using python rstrip
print("r-tripped string:", result.format(string.rstrip("alam")))

Output:

Unstripped string: |bashir alam|
l-tripped string: | alam|
r-tripped string: |bashir |

 

Examples of stripping special characters from a string

So far we have learned how we can remove the alphabets from the beginning and ending of a string using the python strip method. In this section, we will take a look at how we can remove the special characters including symbols and digits from the given characters. See the example below, which removes the special symbols from the string.

# string
string = "$100 dollar?"
result ="|{}|"

# printing unstripped string
print("Unstripped string:",result.format(string))

# printing the stripped string using python strip
print("stripped string:", result.format(string.lstrip("$")))

Output:

Unstripped string: |$100 dollar?|
stripped string: |100 dollar?|

Notice that we have successfully removed the dollar symbol from our string using the python strip method. Now let us use lstrip and rstrip method to remove the dollar and question mark from the string. See the example below:

# string
string = "$100 dollar?"
result ="|{}|"

# printing unstripped string
print("Unstripped string:",result.format(string))

# printing the stripped string using python lstrip
print("l-stripped string:", result.format(string.lstrip("$")))

# printing the stripped string using python rstrip
print("r-stripped string:", result.format(string.rstrip("?")))

Output:

Unstripped string: |$100 dollar?|
l-stripped string: |100 dollar?|
r-stripped string: |$100 dollar|

In a similar way, we can removes digits from the string using the python strip method. See the following example which removes the country code from the given number.

result ="|{}|"

# printing unstripped string
print("Unstripped string:",result.format(string))

# printing the stripped string using python strip
print("stripped string:", result.format(string.strip("+996")))

Output:

Unstripped string: |+9962453424|
stripped string: |2453424|

In a similar way, if want, we can use lstrip and rstrip to removes the digits from the left and right sides as well.

 

Summary

The python strip method is used to remove characters from the beginning and ending of the string. Python also supports only removing string characters from the beginning and ending only by using python lstrip and rstrip methods. In this tutorial, we learned about the python strip, lstrip and rstip methods in detail. We learned how we can remove whitespaces, special characters, and numeric values from the starting and ending of a string. Moreover, we also covered the syntax and returned data type of the python strip method. All in all, this tutorial contains all details about the python strip method.

 

Further Reading

Python strip method
Python strings
Python built-in functions

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment