# Ways To Remove Whitespaces From The String In Python With Examples - Beginner's Guide

## Introduction

If you have learned about **Python string** then you would know that -

Python String is **immutable** i.e., **it cannot be changed**. When we try to manipulate string using any functions or methods, it returns the modified string and not the original string and we work on that modified string in our program.

In this tutorial, we are going to discuss **some of the ways to remove whitespace characters from our string using various methods and functions**.

We are going to see the following methods:

* `str.strip()`
    
* `str.replace()`
    
* `str.split()`
    
* `str.lstrip() and str.rstrip()`
    
* `re.sub()`
    

Let's get started and see the above-mentioned functions one by one.

## strip()

The first thing that clicked in our mind to remove whitespace characters is to use the [`str.strip()`](https://docs.python.org/3/library/stdtypes.html#str.strip) function.

It removes the **leading** and **trailing** whitespace characters which means **it removes the whitespace from the beginning and the end of the string**.

```plaintext
my_str = "      G e e k P y t h o n.      "

output = my_str.strip()
print(f"Removed whitespaces using strip: {output}")
```

**Output**

```plaintext
>>> Removed whitespaces using strip: G e e k P y t h o n.
```

`strip()` method removed the leading and the trailing whitespace from the `my_str`.

## replace()

Python [`replace()`](https://docs.python.org/3/library/stdtypes.html#str.replace) function **removes all the whitespace characters from the string including whitespace between words too**.

```plaintext
my_str = "      G e e k P y t h o n.      "

output = my_str.replace(" ", "")
print(f"Removed whitespaces using replace: {output}")
```

**Output**

```plaintext
>>> Removed whitespaces using replace: GeekPython.
```

Here, we replaced all the spaces in the string with no space, and hence, all the whitespace was removed successfully.

## split()

We can use [`split()`](https://docs.python.org/3/library/stdtypes.html#str.split) function to remove duplicate whitespace characters from the string but we have to take help from `join()` function.

```plaintext
my_str = "      G e e k P y t h o n.      "

output = " ".join(my_str.split())
print(f"Removed whitespaces using split: {output}")
```

**Output**

```plaintext
>>> Removed whitespaces using split: G e e k P y t h o n.
```

## lstrip() & rstrip()

If you recall the `str.strip()` method, it removes leading and trailing whitespace from the string.

[`lstrip()`](https://docs.python.org/3/library/stdtypes.html#str.lstrip) and [`rstrip()`](https://docs.python.org/3/library/stdtypes.html#str.rstrip) - just think of them like the strip() method is divided into two parts.

`lstrip()` - **removes the leading whitespace characters** from the string.

`rstrip()` - **removes the trailing whitespace characters** from the string.

```plaintext
my_str = "      G e e k P y t h o n.      "

output = my_str.lstrip()
print(f"Using l-strip method: {output}")

output1 = my_str.rstrip()
print(f"Using r-strip method: {output1}")
```

**Output**

```plaintext
>>> Using l-strip method: G e e k P y t h o n.      
>>> Using r-strip method:       G e e k P y t h o n.
```

## Using regex

We can use a regular expression to remove whitespace by matching the whitespace characters and then substitute using [`re.sub()`](https://docs.python.org/3/library/re.html#re.sub) method.

The syntax will be simple

> **import re**

> **my\_str = re.sub(r"\\s+", "", sentence)**

Here, `"r"` in the beginning means that the string should be treated as **"raw string"**.

And `"\s+"` means that the whitespace character with one or more occurrences.

Let's see some examples

**To remove all the whitespace characters**

```plaintext
import re

my_str = "      G e e k P y t h o n.      "

# Removed all the whitespaces
output = re.sub(r"\s+", "", my_str)
print(f"Removed all the whitespaces: {output}")
```

**Output**

```plaintext
>>> Removed all the whitespaces: GeekPython.
```

Here, we substituted all the whitespace with no space.

**To remove whitespace from the BEGINNING**

```plaintext
import re

my_str = "      G e e k P y t h o n.      "

# Removed whitespace from the start
output1 = re.sub(r"^\s+", "", my_str)
print(f"Removed whitespace from the start: {output1}")
```

**Output**

```plaintext
>>> Removed whitespace from the start: G e e k P y t h o n.
```

Here, we used `"^\s+"` regex pattern to remove whitespace from the beginning of the string.

**To remove whitespace from the END**

```plaintext
import re

my_str = "      G e e k P y t h o n.      "

# Removed whitespace from the end
output2 = re.sub(r"\s+$", "", my_str)
print(f"Removed whitespace from the end: {output2}")
```

**Output**

```plaintext
>>> Removed whitespace from the end:       G e e k P y t h o n.
```

Here, we used `"\s+$"` regex pattern to remove whitespace from the end of the string.

**Remove whitespaces both in the BEGINNING and at the END of a string**

```plaintext
import re

my_str = "      G e e k P y t h o n.      "

# Remove spaces both in the BEGINNING and at the END of a string:
output3 = re.sub(r"^\s+|\s+$", "", my_str)
print(f"Removed spaces both in the BEGINNING and at the END of a string: {output3}")
```

**Output**

```plaintext
>>> Removed spaces both in the BEGINNING and at the END of a string: G e e k P y t h o n.
```

Here, we used `"^\s+|\s+$"` regex pattern to remove whitespace from the beginning and from the end of the string.

**To remove DUPLICATE spaces**

```plaintext
import re

my_str = "      G e e k P y t h o n.      "

# Remove DUPLICATE spaces:
output4 = " ".join(re.split("\s+", my_str))
print(f"Removed DUPLICATE spaces: {output4}")
```

**Output**

```plaintext
>>> Removed DUPLICATE spaces:  G e e k P y t h o n.
```

If you recall using, `.join()` method with `str.split()` - here, we are using same method instead we used [`re.split()`](https://docs.python.org/3/library/re.html#re.split) function.

## Conclusion

You've learned some of the ways to clean up your Python string and apply them when you were working on your project where you work on textual data.

Out of the above-discussed ways, you can prefer any method based on your requirement and comfort.

If you want to learn about the above-discussed functions in deep then you can visit the official documentation of Python [string methods](https://docs.python.org/3/library/stdtypes.html#string-methods).

---

**That's all for now.**

**Keep Coding✌✌**
