The strip() Function in Python

Last updated by Utkarsh Sahu on Sep 22, 2025 at 11:28 PM
| Reading Time: 3 minutes

Python supports object-oriented programming and has a concise, readable, and easy-to-learn syntax. It is no wonder that it is one of the most popular programming languages. The strip() function in Python is one of its many integral and built-in functions.

In this article, we will explain what is the strip() function in Python and what role it plays. We also have presented the syntax of this function and have provided a few examples to help you understand it better.

What is The Strip() Function in Python and What Does it Do?

Python’s strip() function is a built-in function that removes specific leading and trailing characters from the start and end of the original string. These characters to remove are given as an argument to strip() for removal.

If we provide no string as an argument, then the strip() function removes leading and trailing whitespaces by default. If no leading or trailing strings match the argument, the new string returned is the same as the original string.

The strip() Function in Python: Syntax


string.strip([characters])

Parameter: 

characters (optional): Set of characters to remove from the start and end of the original string.

Return Value:

Returns a new string that’s a copy of the original string with required leading and trailing characters removed.

The Strip() Function in Python 3: Example

Here, we take a look at how you can use the Python function strip() the next time you need it:

Code to Explain the Working of Strip() in Python

Here’s a basic example to understand the working of the strip() function in Python:


stringExample = "interview inter interinter"
print(stringExample.strip("inter"))

From the start:

  • The first instance of “inter” is found in “interview” and removed.
  • The next instance is checked at “v,” and since that’s a mismatch, removal from the start ends there.
  • The string now starts with “v.”

From the end:

  • The last “inter” in “interinter” is removed first.
  • The next instance is checked at “r” in “interinter,” which is found and removed.
  • The next check happens at the space “ “ between “inter” and “interinter.” Since that’s a mismatch, removal from the end ends there.
  • The string now ends with a space.

Output

Note that the search from the start or end stops as soon as there’s a mismatch in the original string.


view inter

Code to Demonstrate the Working of Strip() with more Examples

Output

# Using strip() in Python 3

# Default strip based on space
stringExample1 = "    This is *****Interview Kickstart******     !    "
print("Original string 1: "+ stringExample1)

strippedString1 = stringExample1.strip()
print("By default strip() strips spaces from the left and right ends: "+ strippedString1+ "n")

# Stripping based on a letter, strip() is case sensitive, removes instances only at the ends
stringExample2 = "This is Interview Kickstart"
print("Original string 2: "+ stringExample2)

strippedString2 = stringExample2.strip("t")
print("Strip t from the left and right ends only removes lowercase t: "+ strippedString2+ "n")

# Stripping example using symbols


# Using a symbol in the middle (!)
stringExample3 = "****This is Interview Kickstart!****"
print("Original string 3: "+ stringExample3)

strippedString3 = stringExample3.strip("!")
print("Since ! is not at the end, strip() doesn't remove it: "+ strippedString3)

# Using a symbol at the end that repeats at the ends several times(*). strip() only strips all instances of the symbol at the ends
strippedString4 = stringExample3.strip("*")
print("Strip * from the left and right ends: "+ strippedString4)
Output

Original string 1:     This is *****Interview Kickstart******     !    
By default strip() strips spaces from the left and right ends: This is *****Interview Kickstart******     !

Original string 2: This is Interview Kickstart
Strip t from the left and right ends only removes lowercase t: This is Interview Kickstar

Original string 3: ****This is Interview Kickstart!****
Since ! is not at the end, strip() doesn't remove it: ****This is Interview Kickstart!****
Strip * from the left and right ends: This is Interview Kickstart!

 

Nail Your Machine Learning Interview with Interview Kickstart!

Interview Kickstart’s Machine Learning course is designed to help you ace the interview and land your dream job. Enrol in this course to learn the key concepts of machine learning such as data pipelines, supervised and unsupervised learning, NLP, deep learning, etc.

 

In this course, you will also learn about system design and data structures & algorithms. Our FAANG+ experts will also help you write resumes that can clear the ATS, build a strong online personal brand, and optimize your LinkedIn profile.

 

Read the success stories of our graduates and get inspired to land your dream role.

FAQs on the strip() Function in Python

Q1. What is the Use of the Strip() Function in Python?

The strip() method removes characters from both the left and right ends based on the optional argument we can pass to strip(). The optional argument is a string specifying the set of characters to remove if present at the start or the end.

Q2. How Do You Strip Whitespace in a Line in Python?

In Python, we can use some string functions like strip() to remove extra spaces from both ends, lstrip() to remove from only the left end, and rstrip() to remove from the right end only. Since we cannot modify strings in place, all these three functions return new strings with the necessary leading and trailing characters removed.

Q3. How Would You Strip Whitespaces in Python?

Python’s string function called strip() removes both leading and trailing whitespaces. To remove only leading or trailing spaces, we can use lstrip() for leading spaces and rstrip() function for trailing spaces.

Q4. Does Strip() Remove Newline Python?

Python’s strip() function can remove n, a newline character, from a string. The strip() function can remove both trailing and leading newline characters from the original string.

Q5. What is the Difference Between Python’s Strip() and Split() Functions with Respect to Whitespaces?

There’s no practical difference as split() by default ignores trailing whitespace at the end of the input. If someone calls strip() first and then uses split(), they either don’t know this behavior of split(), or they’re just doing it for clarity.

Related reads:

  • Top 20 Deep Learning Interview Questions
  • Top Agile Interview Questions
Last updated on: September 22, 2025
Register for our webinar

Uplevel your career with AI/ML/GenAI

Loading_icon
Loading...
1 Enter details
2 Select webinar slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Strange Tier-1 Neural “Power Patterns” Used By 20,013 FAANG Engineers To Ace Big Tech Interviews

100% Free — No credit card needed.

Register for our webinar

Uplevel your career with AI/ML/GenAI

Loading_icon
Loading...
1 Enter details
2 Select webinar slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

IK courses Recommended

Master ML interviews with DSA, ML System Design, Supervised/Unsupervised Learning, DL, and FAANG-level interview prep.

Fast filling course!

Get strategies to ace TPM interviews with training in program planning, execution, reporting, and behavioral frameworks.

Course covering SQL, ETL pipelines, data modeling, scalable systems, and FAANG interview prep to land top DE roles.

Course covering Embedded C, microcontrollers, system design, and debugging to crack FAANG-level Embedded SWE interviews.

Nail FAANG+ Engineering Management interviews with focused training for leadership, Scalable System Design, and coding.

End-to-end prep program to master FAANG-level SQL, statistics, ML, A/B testing, DL, and FAANG-level DS interviews.

Select a course based on your goals

Agentic AI

Learn to build AI agents to automate your repetitive workflows

Switch to AI/ML

Upskill yourself with AI and Machine learning skills

Interview Prep

Prepare for the toughest interviews with FAANG+ mentorship

Ready to Enroll?

Get your enrollment process started by registering for a Pre-enrollment Webinar with one of our Founders.

Next webinar starts in

00
DAYS
:
00
HR
:
00
MINS
:
00
SEC

Register for our webinar

How to Nail your next Technical Interview

Loading_icon
Loading...
1 Enter details
2 Select slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Almost there...
Share your details for a personalised FAANG career consultation!
Your preferred slot for consultation * Required
Get your Resume reviewed * Max size: 4MB
Only the top 2% make it—get your resume FAANG-ready!

Registration completed!

🗓️ Friday, 18th April, 6 PM

Your Webinar slot

Mornings, 8-10 AM

Our Program Advisor will call you at this time

Register for our webinar

Transform Your Tech Career with AI Excellence

Transform Your Tech Career with AI Excellence

Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills

25,000+ Professionals Trained

₹23 LPA Average Hike 60% Average Hike

600+ MAANG+ Instructors

Webinar Slot Blocked

Interview Kickstart Logo

Register for our webinar

Transform your tech career

Transform your tech career

Learn about hiring processes, interview strategies. Find the best course for you.

Loading_icon
Loading...
*Invalid Phone Number

Used to send reminder for webinar

By sharing your contact details, you agree to our privacy policy.
Choose a slot

Time Zone: Asia/Kolkata

Choose a slot

Time Zone: Asia/Kolkata

Build AI/ML Skills & Interview Readiness to Become a Top 1% Tech Pro

Hands-on AI/ML learning + interview prep to help you win

Switch to ML: Become an ML-powered Tech Pro

Explore your personalized path to AI/ML/Gen AI success

Your preferred slot for consultation * Required
Get your Resume reviewed * Max size: 4MB
Only the top 2% make it—get your resume FAANG-ready!
Registration completed!
🗓️ Friday, 18th April, 6 PM
Your Webinar slot
Mornings, 8-10 AM
Our Program Advisor will call you at this time

Get tech interview-ready to navigate a tough job market

Best suitable for: Software Professionals with 5+ years of exprerience
Register for our FREE Webinar

Next webinar starts in

00
DAYS
:
00
HR
:
00
MINS
:
00
SEC

Your PDF Is One Step Away!

The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants

The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer

The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary