Home » Python » Python String strip() Method

About Yatin

Working as a Technical Lead in the Information Technology sector where I'm primarily involved with projects based on Java/J2EE Technologies platform (such as Spring, Spring Boot, Hibernate, Redis, etc.), novel UI technologies (Angular & React), Docker, and AWS. Recently completed the AWS Solution Architect Associate Certification.

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

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