---
description: In this Python String strip() function tutorial, you will learn What is strip() function, Syntax, Why use strip() function with examples.
title: "Python String strip() Function: How to use?"
image: https://www.guru99.com/images/python-string-strip-function.png
---

[Skip to content](#main)

**⚡ Smart Summary**

Python strip() removes leading and trailing characters from a string and returns a clean copy. By default it deletes whitespace, while an optional character argument removes any of the specified characters from both ends of the original string.

- 🧹 **Whitespace removal:** strip() deletes spaces, tabs, and newlines from both ends when no argument is passed.
- 🎯 **Character set:** A string argument such as strip(“\*”) removes any of those characters, never a fixed substring.
- ⬅️ **lstrip():** The lstrip() method trims characters from the left side, or start, of the string only.
- ➡️ **rstrip():** The rstrip() method trims characters from the right side, or end, of the string only.
- 🔒 **Immutability:** strip() returns a new string because Python strings cannot be modified in place.
- 🤖 **AI assistance:** AI coding assistants generate strip() cleaning code and normalize text for machine learning and NLP.

Read More

![Python String strip() Function](https://www.guru99.com/images/python-string-strip-function.png)

## What is Python strip()?

**Python strip()** is one of the built-in functions available in the Python library. The strip() method removes the given characters from the start and end of the original string. By default, strip() removes white spaces from the start and end of the string and returns the same string without the surrounding white spaces.

### Syntax of the strip() method

```
string.strip([characters])

```

#### Parameters

- **characters**: (optional) The given characters will be removed from the start and end of the original string.
- If the characters parameter is not given, the white spaces from the start and end of the string will be removed.

#### Return Value

The Python String strip() method will return:

- The original string with white spaces removed from the start and end if the characters to be removed are not specified.
- In case the string does not have any white spaces at the start or end, the string will be returned as it is and will match the original string.
- If the characters parameter is given and the characters match, the characters at the start or end of the string will be removed, and the rest of the string will be returned.
- In case the characters given do not match the start or the end of the original string, the string will be returned as it is.

## Examples of strip() Function in Python

The following examples show how the strip() method behaves with default whitespace, with an invalid data type, and with custom character parameters.

### Example 1: strip() Method in Python

```
str1 = "Welcome to Guru99!"
after_strip = str1.strip()

```

**Output:**

```
Welcome to Guru99!
```

### Example 2: strip() on Invalid Data Type

The Python String strip() function works only on strings and will return an error if used on any other data type, such as a list or tuple.

Example when used on a list():

```
mylist = ["a", "b", "c", "d"]
print(mylist.strip()) 

```

The above will throw an error:

```
Traceback (most recent call last):
  File "teststrip.py", line 2, in <module>
    print(mylist.strip())
AttributeError: 'list' object has no attribute 'strip'

```

**Don't Miss:**

- [Python XML File – How to Read, Write & Parse](https://www.guru99.com/manipulating-xml-with-python.html)
- [Copy File in Python: shutil.copy(), shutil.copystat() method](https://www.guru99.com/python-copy-file.html)
- [Python Lambda Functions with EXAMPLES](https://www.guru99.com/python-lambda-function.html)
- [Yield in Python Tutorial: Generator & Yield vs Return Example](https://www.guru99.com/python-yield-return-generator.html)

### Example 3: strip() Without character parameter

```
str1 = "Welcome to Guru99!"
after_strip = str1.strip()
print(after_strip)

str2 = "Welcome to Guru99!"
after_strip1 = str2.strip()
print(after_strip1)

```

**Output:**

```
Welcome to Guru99!
Welcome to Guru99!

```

### Example 4: strip() Passing character parameters

```
str1 = "****Welcome to Guru99!****"
after_strip = str1.strip("*")
print(after_strip)

str2 = "Welcome to Guru99!"
after_strip1 = str2.strip("99!")
print(after_strip1)
str3 = "Welcome to Guru99!"
after_strip3 = str3.strip("to")
print(after_strip3)

```

**Output:**

```
Welcome to Guru99!
Welcome to Guru
Welcome to Guru99!

```

## Python lstrip() and rstrip() Methods

The strip() method trims both ends of a string at once, but Python also provides two one-sided variants. The lstrip() method removes characters only from the left, or start, of the string, while the rstrip() method removes characters only from the right, or end. Like strip(), both remove whitespace by default and accept an optional set of characters to strip.

```
str1 = "***Guru99***"
print(str1.lstrip("*"))
print(str1.rstrip("*"))
```

**Output:**

```
Guru99***
***Guru99
```

The three methods differ only in which side they trim. Applied to the padded string ” Guru99 “, the results are:

- **strip()** removes whitespace from both ends, returning “Guru99”.
- **lstrip()** removes whitespace from the left only, returning “Guru99 “.
- **rstrip()** removes whitespace from the right only, returning ” Guru99″.

## Why Python strip() function is used?

Here are the main reasons for using the Python strip() function:

- It helps to remove the characters at the start and the end of the string based on the characters given to be removed from the original string.
- If the characters given do not match the original string, the string will be returned as it is.
- If the characters to be removed are not specified, the white spaces from the start and end of the original string will be removed.
- If there is no white space at the start or end, the string will be returned as it is.

» Learn more about [Python String methods](https://www.guru99.com/learning-python-strings-replace-join-split-reverse.html)

This code is editable. Click Run to Execute  

  

## FAQs

🧹 Does Python strip() remove characters from the middle of a string?

No. strip() only removes characters from the start and end of a string. To remove characters from the middle or every occurrence, use the replace() method instead, which scans the entire string.

🔁 What is the difference between strip() and replace() in Python?

strip() trims characters only from the two ends of a string, while replace() substitutes every matching occurrence throughout the whole text. Using strip() keeps the spaces between words, whereas replace() can delete every space in the string.

🎯 Does strip() remove tabs and newlines as well as spaces?

Yes. Without an argument, strip() removes every leading and trailing whitespace character, including regular spaces, tab characters, and newline characters. Whitespace located between words is left untouched.

🔠 Is the Python strip() method case-sensitive?

Yes. strip() matches characters exactly, so strip(‘W’) removes an uppercase W but leaves a lowercase w in place. Normalize case first with lower() or upper() when you need case-insensitive trimming.

🪞 Is there a trim() function in Python?

No. Python has no trim() method. The equivalent operations are strip() for both ends, lstrip() for the left side, and rstrip() for the right side. Developers moving from Java or JavaScript use these instead of trim().

🤖 How can AI help clean and strip text data for machine learning?

AI coding assistants generate strip(), lstrip(), and rstrip() cleaning code from plain-language prompts. In machine learning and NLP pipelines, stripping stray whitespace normalizes raw text before tokenization, improving data quality and model accuracy.

🧠 Can GitHub Copilot generate Python string-cleaning code with strip()?

Yes. GitHub Copilot autocompletes strip(), lstrip(), and rstrip() calls from a short comment describing your goal. Agentic AI tools go further, refactoring an entire text-cleaning pipeline and then running your tests automatically.

#### Summarize this post with:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/python-string-strip-function.png","url":"https://www.guru99.com/images/python-string-strip-function.png","width":"700","height":"250","caption":"Python String strip() Function","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/python-string-strip.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/python","name":"Python"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/python-string-strip.html","name":"Python String strip() Function: How to use?"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/python-string-strip.html#webpage","url":"https://www.guru99.com/python-string-strip.html","name":"Python String strip() Function: How to use?","dateModified":"2026-10-07T13:31:06+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/python-string-strip-function.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/python-string-strip.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/editorial","name":"Editorial Team","url":"https://www.guru99.com/author/editorial","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/favicon-new-logo.png","url":"https://www.guru99.com/images/favicon-new-logo.png","caption":"Editorial Team","inLanguage":"en-US"},"sameAs":["https://www.guru99.com/editorial-team"],"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Python","headline":"Python String strip() Function: How to use?","description":"In this Python String strip() function tutorial, you will learn What is strip() function, Syntax, Why use strip() function with examples.","keywords":"python","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/editorial","name":"Editorial Team"},"dateModified":"2026-10-07T13:31:06+05:30","image":{"@id":"https://www.guru99.com/images/python-string-strip-function.png"},"copyrightYear":"2026","name":"Python String strip() Function: How to use?","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Does Python strip() remove characters from the middle of a string?","acceptedAnswer":{"@type":"Answer","text":"No. strip() only removes characters from the start and end of a string. To remove characters from the middle or every occurrence, use the replace() method instead, which scans the entire string."}},{"@type":"Question","name":"What is the difference between strip() and replace() in Python?","acceptedAnswer":{"@type":"Answer","text":"strip() trims characters only from the two ends of a string, while replace() substitutes every matching occurrence throughout the whole text. Using strip() keeps the spaces between words, whereas replace() can delete every space in the string."}},{"@type":"Question","name":"Does strip() remove tabs and newlines as well as spaces?","acceptedAnswer":{"@type":"Answer","text":"Yes. Without an argument, strip() removes every leading and trailing whitespace character, including regular spaces, tab characters, and newline characters. Whitespace located between words is left untouched."}},{"@type":"Question","name":"Does strip('99!') remove a substring or individual characters?","acceptedAnswer":{"@type":"Answer","text":"It removes individual characters. The argument is treated as a set of characters, so strip('99!') strips any 9 or ! sitting at either end until a different character appears. It never matches the exact sequence."}},{"@type":"Question","name":"Is the Python strip() method case-sensitive?","acceptedAnswer":{"@type":"Answer","text":"Yes. strip() matches characters exactly, so strip('W') removes an uppercase W but leaves a lowercase w in place. Normalize case first with lower() or upper() when you need case-insensitive trimming."}},{"@type":"Question","name":"Is there a trim() function in Python?","acceptedAnswer":{"@type":"Answer","text":"No. Python has no trim() method. The equivalent operations are strip() for both ends, lstrip() for the left side, and rstrip() for the right side. Developers moving from Java or JavaScript use these instead of trim()."}},{"@type":"Question","name":"How can AI help clean and strip text data for machine learning?","acceptedAnswer":{"@type":"Answer","text":"AI coding assistants generate strip(), lstrip(), and rstrip() cleaning code from plain-language prompts. In machine learning and NLP pipelines, stripping stray whitespace normalizes raw text before tokenization, improving data quality and model accuracy."}},{"@type":"Question","name":"Can GitHub Copilot generate Python string-cleaning code with strip()?","acceptedAnswer":{"@type":"Answer","text":"Yes. GitHub Copilot autocompletes strip(), lstrip(), and rstrip() calls from a short comment describing your goal. Agentic AI tools go further, refactoring an entire text-cleaning pipeline and then running your tests automatically."}}]}],"@id":"https://www.guru99.com/python-string-strip.html#schema-1140194","isPartOf":{"@id":"https://www.guru99.com/python-string-strip.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/python-string-strip.html#webpage"}}]}
```
