# 15 Easy Ways to Trim a String in Python

I'm not gonna lie. There are multiple ways you can trim a string in Python.

But... the truth is, you don't need to know every one of them.

In this article, you'll see only the most important techniques, such as stripping leading and trailing spaces (as well as the ones inside the string). You'll also learn how to remove tabs, newlines, carriage return (CRLF), and other characters. And we'll be using nothing more than native methods and regex—no external libraries required!

By the end of this article, you'll have mastered:

- [How to trim a string](#how-to-trim-characters-from-a-string)

    - [by stripping leading whitespace from the beginning](#stripping-leading-whitespace-from-beginning-of-a-string)

    - [by stripping trailing whitespace from the end](#stripping-trailing-whitespace-from-end-of-a-string)

    - [by removing spaces the start and end of a string](#removing-spaces-from-from-start-and-end-of-a-string)

- [How trim newlines](#how-to-trim-newlines)

- [How trim carriage return (CRLF)](#how-to-trim-newlines)

- [How trim tabs](#how-to-trim-tabs)

- [How to trim a combination of characters from a string](#how-to-remove-multiple-spaces-inside-a-string)
- [How to remove multiple spaces inside a string](#how-to-remove-multiple-spaces-inside-a-string)

    - [by removing only duplicates](#removing-only-duplicates)

    - [by removing all spaces](#removing-all-spaces)

- [How to strip a list of strings](#how-to-strip-a-list-of-strings)
- [How to strip an (Numpy) array of strings](#how-to-strip-an-numpy-array-of-strings)


## How to Trim Characters From a String

Trimming a string means deleting certain chars from the start, the end, or both sides of a string. Removing unwanted chars makes it easier to [compare strings](https://miguendes.me/python-compare-strings) and can prevent hard to debug issues.

You can remove any kind o character, but usually what we're interested in is deleting blank spaces, new lines, carriage return (CRLF), tabs and other special symbols.

In this section, we're going to see how to remove leading or trailing spaces, blank spaces, newline character, carriage return (CRLF), and tabs.

### Stripping Leading Whitespace From Beginning of a String

The `str` class has a very convenient method to trim leading spaces named [`str.lstrip`](https://docs.python.org/3/library/stdtypes.html#str.lstrip), a shorthand for "left-strip", since it trims a string from the left-hand side. You can think of it as a left trim.

```python
>>> '   hello   '.lstrip()
'hello   '
```
![using python .lstrip method to remove leading spaces from a string](https://cdn.hashnode.com/res/hashnode/image/upload/v1631520853178/hYHhQ7pef.png)

When calling `str.lstrip` with no arguments, it removes all whitespaces from left to right. But if all you want is to strip the first char, then there are two ways of doing this. The first one assumes that there will always be at least one whitespace in the beginning of the string. If that's the case, then you can just slice it.

```python
>>> s = '  hello'
>>> s = s[1:]
>>> s
' hello'
```
If there's no guarantee of that, we'll need to check first if the string starts with space.

``` python
>>> def strip_first(s: str, ch: str = ' ') -> str:
     if s and s[0] == ch:
         return s[1:]
     return s

>>> strip_first('hello')
'hello'

>>> strip_first('   hello')
 '  hello'
```

### Stripping Trailing Whitespace From End of a String

The way to remove trailing spaces from the end of the string is to use [`str.rstrip`](https://docs.python.org/3/library/stdtypes.html#str.rstrip). 

This method expects a list of *chars* and trims the string from the right. It removes all chars that match one of those you passed, and stop as soon as it cannot match anymore. By default, `str.rstrip()` removes blanks if you don't pass anything to it. You can think of it as a right trim.

```python
>>> '   hello   '.rstrip()
'   hello'
>>> '***hello***'.rstrip('*')
'***hello'
```

![using python .rstrip method to remove trailing spaces from the end of a string](https://cdn.hashnode.com/res/hashnode/image/upload/v1631521195422/3V9MhcO_a.png)

Sometimes you might want to trim only the last character of a string. And we can use the same logic from the previous example. Check if the last char is a space, and use slice to remove it.

```python
>>> def strip_last(s: str, ch: str = ' ') -> str:
     if s and s[-1] == ch:
         return s[:-1]
     return s


>>> strip_last('hello')
'hello'

>>> strip_last('hello ')
'hello'

>>> strip_last('')
''
```

### Removing Spaces From From Start and End of a String

If all you want is to remove whitespaces from start and end of string, [`str.strip`](https://docs.python.org/3/library/stdtypes.html#str.strip) will serve you better. 

This method trims both sides of the string. And just like `str.lstrip` and `str.rstrip`, if you can pass any combination of chars as argument, it removes them from both ends.

> ⚠️ WARNING ⚠️: A common misconception is to think that there's a trim() function in Python.

```python
# by default, strip removes whitespaces
>>> '   hello   '.strip()
'hello'
# but you can also strip other character
>>> '***hello***'.strip('*')
'hello'
```

![using python .strip to remove spaces from both sides of a string](https://cdn.hashnode.com/res/hashnode/image/upload/v1631521436839/b8jUI1YxQ.png)

## How to Trim Newlines

We've seen how `str.strip` can remove blank spaces from both sides of a string. I've also mentioned that this method takes a chars argument that you can use pass a combination of character you want to trim. 

To trim line breaks, you can pass `\n` and it will strip all newlines from both sides of the string.

```python
>>> s = """
... 
... 
...  hello
... 
... 
... """
>>> s
'\n\n\n hello\n\n\n'
>>> s.strip('\n')
' hello'
```

![strip new lines from a string using .strip method](https://cdn.hashnode.com/res/hashnode/image/upload/v1631522146211/eWAv1hx_1m.png)

## How to Trim Carriage Return (CRLF)

The Carriage Return (*CR*), and Line Feed (*LF*) are nothing more than a newline character. They are represented by the concatenation of `\r` and `\n` forming `\r\n`. This is how Microsoft Windows, Symbian OS and other non-Unix operating systems represent a new line [[source]](https://stackoverflow.com/a/1552782).

Removing them from a string is the same as removing the single newline. You feed `str.strip` with `\r\n` and method does its job!

```python
>>> s = "  hello world\r\n\r\n"
>>> print(s)
  hello world


>>> s.strip('\r\n')
'  hello world'
```

![trimming carriage return - line feed (CRLF) from a string in python](https://cdn.hashnode.com/res/hashnode/image/upload/v1631523169122/tXciFhqkn.png)

## How to Trim Tabs

If you are following this guide from the beginning you might already know how to do this. Trimming tabs from a string in Python is the same as other characters, you use `str.strip` and pass the '\t' string to it.


```python
>>> s = "\t\t\t  hello  world \t"       
>>> s
'\t\t\t  hello  world \t'
>>> print(s)
			  hello  world 	
>>> s.strip('\t')
'  hello  world '
```
![stripping tabs from a string using strip method in python](https://cdn.hashnode.com/res/hashnode/image/upload/v1631523357785/9OWG3y_bh.png)

And that's it!

## How to Trim a Combination of Characters From a String

As I mentioned before, `str.strip` takes as argument a string, not just a single char. This sequence of chars is a combination of all chars you want to remove from the beginning and end of your string.

```python
>>> s = "  \ns hello world \n    s"
>>> s    
'  \ns hello world \n    s'
>>> print(s)
  
s hello world 
    s
>>> s.strip('\n s')
'hello world'
```
![trimming a combination of more than one char from both sides of a string in python](https://cdn.hashnode.com/res/hashnode/image/upload/v1631695493856/iYeyK5RyL.png)

## How to Remove Multiple Spaces Inside a String

Sometimes you want to do more than trimming, let's say you want to remove chars inside the string. There are two ways of doing this: one is to remove only the duplicates; the other is to remove all extra spaces.

### Removing Only Duplicates

To remove only the duplicated characters, you can use the regex module [`re`](https://docs.python.org/3/library/re.html)

```python
>>> import re
>>> s = "   Python   is really   a    great language.    "
>>> re.sub("\s+" , " ", s)
' Python is really a great language. '
```

![removing duplicate spaces in a string in python using re module](https://cdn.hashnode.com/res/hashnode/image/upload/v1631690536081/xryOFAo3I.png)

This method gets rid of all consecutive spaces. What if you want to do not only that, but also trim the string by removing the leading and trailing blanks?

One way is to split the string and then joining then like so:

```python
>>> s = "   Python   is really   a    great language.    "
>>> " ".join(s.split())
'Python is really a great language.'
>>> # This is the same as using regex then stripping the whitespaces
>>> re.sub("\s+" , " ", s).strip()
'Python is really a great language.'
```

### Removing All Spaces

Now, if you want to strip all whitespace in your string, either use regex or call the [`str.replace`](https://docs.python.org/3/library/stdtypes.html#str.replace) method.

#### Using `re` (regex module)

```python
>>> import re
>>> s = "   Python   is really   a    great language.    "
>>> re.sub("\s+" , "", s) 
'Pythonisreallyagreatlanguage.'
```
![removing all spaces from a string in python using regex](https://cdn.hashnode.com/res/hashnode/image/upload/v1631690673798/4pWbyJlMu.png)

#### Using `replace`

```python
>>> s = "   Python   is really   a    great language.    "
>>> s.replace(' ', '')
'Pythonisreallyagreatlanguage.'
```
![removing all spaces from a string using .replace](https://cdn.hashnode.com/res/hashnode/image/upload/v1631690935792/4dTCkSt_e.png)

## How to Strip a List of Strings

Trimming a list of strings is almost the same as trimming an individual one. The only difference is that you have to iterate over the list, and call `str.strip` method on each one. You do so by using a list comprehension, for example, to return a new list with all strings trimmed.

```python
>>> lst = ["string1\n", "string2\n", "string3\n"]
>>> [s.strip('\n') for s in lst]
['string1', 'string2', 'string3']
```

## How to Strip an (Numpy) Array of Strings

It's very common to use [Numpy](https://numpy.org) for data science tasks due to its performance and ease to use. 

If you have a array of strings and want to trim each one of them, Numpy comes with an efficient vectorized implementation of [`strip`](https://numpy.org/doc/stable/reference/generated/numpy.char.strip.html#numpy.char.strip). 

In fact, it also has `.lstrip`, `.rstrip`, `.replace`, and many other [string operations](https://numpy.org/doc/stable/reference/routines.char.html).

The vectorized versions work slightly differently, they are not a method but a function in the `numpy.char` module. So you must pass the array and the list of chars you want to trim.

```python
>>> import numpy as np
>>> arr = np.array([' helloworld   ', ' hello'])
array([' helloworld   ', ' hello'], dtype='<U7')
>>> np.char.strip(arr, ' ')
array(['helloworld', 'hello'], dtype='<U7')
```
![python_numpy_1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631692401628/hWOSR_Yw7.png)

## Conclusion

In this post, you learned several ways of trimming a string in Python, including array of strings. Python allows us to strip leading and trailing characters easily. And if instead of removing the extra chars on each side you want to remove the ones internally, you can count on the [regex module](https://docs.python.org/3/library/re.html). I hope you've found this article helpful and see you next time! 

References:

https://stackoverflow.com/questions/761804/how-do-i-trim-whitespace-from-a-string

https://stackoverflow.com/questions/8270092/remove-all-whitespace-in-a-string

https://stackoverflow.com/questions/1546226/is-there-a-simple-way-to-remove-multiple-spaces-in-a-string

Other posts you may like:

- [How to Choose Between isdigit(), isdecimal() and isnumeric() in Python](https://miguendes.me/python-isdigit-isnumeric-isdecimal)

- [How to Compare Two Strings in Python (in 8 Easy Ways)](https://miguendes.me/python-compare-strings)

- [Pylint: How to fix "c0209: formatting a regular string which could be a f-string (consider-using-f-string)"](https://miguendes.me/pylint-consider-using-f-string)

- [How to Implement a Random String Generator With Python](https://miguendes.me/how-to-implement-a-random-string-generator-with-python)

- [How to Check If a String Is a Valid URL in Python](https://miguendes.me/how-to-check-if-a-string-is-a-valid-url-in-python)

- [Python F-String: 73 Examples to Help You Master It](https://miguendes.me/73-examples-to-help-you-master-pythons-f-strings)

See you next time!

This post was originally published at [https://miguendes.me](https://miguendes.me/python-trim-string)
