Python

Python String strip() Method

Hello in this tutorial, we will understand how to use the string strip() method in python programming.

1. Introduction

The string strip() method in python programming removes the leading and trailing characters from the given string based on the argument passed to the method. The method returns a copy of the original string after the operation and is represented by the following syntax –

Method syntax

string.strip([characters])

Where,

  • characters param is optional and it specifies the list of characters to be removed. If this not specified leading and trailing whitespaces are removed from the given string

1.1 How does the method work?

  • When the character on the left of the string mismatches with all the characters specified in the optional argument, it stops removing the leading characters
  • Similarly when the character on the right of the string mismatches with all the characters specified in the optional argument, it stops removing the trailing characters

1.2 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python String strip() Method

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice. Let us dive in with the programming stuff now.

2.1 Python String strip() Method

Let us understand the different use cases of the string strip() method in python programming.

strip() method

random_string = '                happy   birthday  , john!       '
# removing leading and trailing whitespaces
print(random_string.strip())

random_string1 = '*****python****is**a***high-level_language!*****'
# removing leading and trailing astrix
print(random_string1.strip('*'))

random_string2 = 'www.javacodegeeks.com'
# remove the particular characters
# '.wcom' removes 'www', 'com' and '.'
print(random_string2.strip('.wcom'))

random_string3 = 'https://www.google.com/'
# remove the particular characters
# 'https://' removes 'https' and '/'
print(random_string3.strip('https://'))

If everything goes well the following output will be shown in the IDE console.

Console Output

==== Python String strip() Method ====

happy   birthday  , john!
python****is**a***high-level_language!
javacodegeeks
www.google.com

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • String strip() method in python programming
  • Sample program to understand the string strip() method use cases

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was a tutorial on string strip() method in python programming.

Download
You can download the full source code of this example here: Python String strip() Method

Last updated on Feb. 24th, 2022

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button