Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Python String strip() Method

  • 13 Aug, 2025
  • Com 1
Python String strip() Method

Python strip() is a built-in function that removes any specific characters from a string. By default, it removes any leading and trailing whitespaces to clean the string. The output is a new string with specified characters stripped.

Removing leading and trailing whitespaces from both ends

input_string = "   But, I am at Walmart!  "

print(input_string)

# Output: "   But, I am at Walmart!  "


stripped_output = input_string.strip()

print(stripped_output)

# Output: "But, I am at Walmart!"

In this code, all the leading and trailing spaces have been removed, and the result is a clean string.

It removes all whitespace types (including tabs and newlines by default).

Syntax

string.strip(characters)

Parameters

Argument Description
characters (str or None)

It represents a string specifying the set of characters to remove from both ends of the string. It is a set of individual characters, not a substring.

If omitted or None, all whitespace characters are removed (‘ ‘, \t, \n, \r, \x0b, \x0c).

Removing a specific character

Removing a specific character from a string

Let’s remove the specific character from a string from either the start or the end.

str = "data"

print(str.strip('a'))        

# Output: dat

In the above code, we removed the “a” at the end of the string. The output is just “dat”.

Removing newlines and tabs

Removing newlines and tabs from a string in Python

If the string contains “\t” and “\n” characters, you don’t need to specify them to remove them. They are whitespace characters, and they will be stripped by default.

new_tab = "\n\tHello AppDividend\t\n"

print(new_tab)

# Output: 
#	  Hello AppDividend



stripped_input = new_tab.strip()

print(stripped_input)

# Output:
# Hello AppDividend

Removing multiple characters from the start and end

Removing multiple characters from the start and end

Using the strip() function, we can remove multiple specific characters from a string at the start and end points, but cannot remove them from the middle of the string.

multi_str = "...Hello. World!!!"

print(multi_str.strip('.!'))

# Output: Hello. World

In this code, we removed the three “.” (dots) from the start of the string, but were unable to remove the dot (.) from the middle of the string. It also stripped “!” from the end of the string.

Remember, the argument is a character, which means the method matches any character, not substrings.

Empty string

Stripping an empty string in Python

When the string is empty, the output string will be empty too, since there are no characters.

empty_str = ""

print(empty_str.strip())

# Output: ""

No chars Match → Original string returned.

No chars Match → Original string returned

If no characters match, it will return the original string without any modification. The output is the same as the input.

str = "data"

print(str.strip('me'))        

# Output: data

Removing the whole string

What if the character(s) you want to remove are that whole string? Meaning, let’s say, you want to remove “z” from “zzz”? Well, in this case, the entire string “zzz” will be removed.

str = "zzz"

print(str.strip('z'))        

# Output: ""

The above output shows that it returns an empty string, meaning everything is removed.

Removing Substrings vs. Characters

The string strip() method is not about substring removal; it is about removing special characters, such as whitespaces, from the string at the start and end points.

It cannot remove the characters from the middle of the string.

For proper substring removal, use str.replace() or re.sub() method.

str = "Google"

print(str.strip("Goo"))

# Output: gle

In the above code, we are stripping three characters: G, o, and o. Since all three characters appear at the start of the string, they were removed. Here, “Goo” is removed, not as a substring, but as individual characters.

But why did it not remove “g”? Well, the strip() function is case-sensitive. So, the “G” is not the same as “g” and hence, it did not strip it from the string.

Post Views: 24
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Subtract Two Numbers in Python
How to Skip a Value in Python List

1 Comment

  1. Bilbo

    May 28, 2019 at 4:35 pm

    my precious

    Reply

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend