Python Trim String – rstrip(), lstrip(), strip()
Tags: python, programming, codenewbie, tutorial
Published: 2021-09-21 06:52:04

---
title: Python Trim String – rstrip(), lstrip(), strip()
published: true
date: 2021-09-19 18:57:44 UTC
tags: #python #programming #codenewbie #tutorial
canonical_url: https://itsmycode.com/python-trim-string-rstrip-lstrip-strip/
---

ItsMyCode |

Python provides three methods to trim the whitespaces from the string object. Let’s take a look at each of these functions in detail with examples.

## Difference between strip() vs rstrip() vs lstrip()

The following methods are used to trim the whitespaces from the string.

1. **strip():** The **`strip() `**function removes any  **leading and trailing whitespace** , including tabs (\t), and returns a new string.
2. **rstrip():** The **`rstrip() `**function removes any  **trailing whitespace ** returns a new string that means it eliminates the whitespace at the right side of the string.
3. **lstrip():** The **`lstrip()`** function removes any  **leading whitespace ** returns a new string that means it eliminates the whitespace at the left side of the string.

## strip()

The **`strip()`** function removes any  **leading and trailing whitespace** , including tabs (\t), and returns a new string.

**Syntax: string.strip([chars])**

**Parameter** : 

- **chars (optional) **– a string of characters that need to be **removed from the left and right parts of the string.**

**Return Value: **  Returns a copy of the string with both leading and trailing characters stripped

**Note: ** If char arguments are not provided, all the whitespaces on the leading and trailing side are removed from the string.

### **Example of strip() function  **

```python 
# Leading and trailing whitespaces are removed
text1 = ' Python Programming '
print(text1.strip())

# Remove the whitespace and specified character on
# both leading and trailing end
text3 = ' code its my code '
print(text3.strip(' code'))

# Remove the specified character at 
# both leading and trailing end
text2 = 'code its my code'
print(text2.strip('code'))
```

**Output**

```python 
Python Programming
its my
 its my
```

## rstrip()

The **`rstrip() `**function removes any  **trailing whitespace ** and returns a new string that means it eliminates the whitespace at the right side of the string.

**Syntax: string.rstrip([chars])**

**Parameter: **

- **chars (optional) **– a string of characters that need to be **removed on the trailing end.** (right part of the string)

**Return Value: **  Returns a copy of the string with trailing characters stripped.

**Note: ** If char arguments are not provided, all the whitespaces on the trailing side are removed from the string.

### **Example of rstrip() function  ** 

```python 
# Only trailing whitespaces are removed
text1 = ' Python Programming '
print(text1.rstrip())

# Remove the whitespace and specified character at
# trailing end
text2 = ' code its my code '
print(text2.rstrip(' code'))

# Remove the specified character at 
# trailing end
text3 = 'code its my code'
print(text3.rstrip('code'))
```

**Output**

```python 
   Python Programming
       code its my
code its my
```

## lstrip()

The **`lstrip()`** function removes any  **leading whitespace ** and returns a new string that means it eliminates the whitespace at the left side of the string

**Syntax: ** string.lstrip([chars])

**Parameter** : 

- **chars (optional) **– a string of characters that need to be **removed on the leading end**. (left part of the string)

**Return Value: **  Returns a copy of the string with leading characters stripped.

**Note: ** If char arguments are not provided, all the whitespaces on the leading side are removed from the string.

### **Example of lstrip() function  ** 

```python 
# Only leading whitespaces are removed
text1 = ' Python Programming '
print(text1.lstrip())

# Remove the whitespace and specified character at
# leading end
text2 = ' code its my code '
print(text2.lstrip(' code'))

# Remove the specified character at 
# leading end
text3 = 'code its my code'
print(text3.lstrip('code'))
```

**Output**

```python 
Python Programming   
its my code        
 its my code
```

The post [Python Trim String – rstrip(), lstrip(), strip()](https://itsmycode.com/python-trim-string-rstrip-lstrip-strip/) appeared first on [ItsMyCode](https://itsmycode.com).