# How to trim strings in Python

Python trim functions can be used to remove unnecessary characters and whitespace, making them useful functions for **cleaning up input** and normalising strings.

## Why trim strings in Python?

In Python, you should modify strings to ensure clean data processing and display. This will increase readability and improve the appearance of user interfaces and outputs. Trimming is especially useful when it comes to **deleting unintended whitespace** in user entries and working with external data sources like files and APIs.

You can also use Python trim functions for **data validation**. This ensures that all strings conform to the expected Python string format and your data is consistent. However, keep in mind that trimming strings might result in the loss of relevant information. It’s important to take care when using these functions and only use them in the right situations.

## How to use the Python trim functions `strip()`, `lstrip()`, `rstrip()`

Trimming is the process of **removing certain characters from the beginning or end of a string**. It applies to spaces, tabs, line breaks and any other character defined by the user. In Python, you can use `strip()`, `lstrip()` and `rstrip()` for trimming. Below we’ll introduce each of these methods in detail.

### `strip()`

The `strip()` function is a Python method for strings. It removes whitespace and other characters **from the beginning and end of a string**. It’s best used to tidy up strings and rid them of unnecessary characters before they’re processed in a program. If you execute `strip()` without an argument, it will remove the spaces at the beginning and end of the string:

```python
text = "   example   "
cleaned_text = text.strip()
print(cleaned_text)  # Output: "example"
```

If you want to remove other characters from the beginning and end of a string, you can enter them as an argument:

```python
text = "***example***"
cleaned_text = text.strip("*")
print(cleaned_text)  # Output: "example"
```

In this example, `strip("*")` removes the asterisks from the string.

If you apply `strip("exe")` to the string `example`, you’ll get `ampl` as the output:

```python
text = "example"
cleaned_text = text.strip("exe")
print(cleaned_text)  # Output: "ampl"
```

In this example, the method removes all instances of the letters e, x and e (in this order) from the beginning and end of the string `example`. It continues until it reaches a character that isn’t contained in the sequence `exe`.

### `lstrip()`

The Python trim function `lstrip()` stands for ‘left strip’ and **removes all characters from the left side** of the string. It continues until it reaches a character that should not be removed. Without an argument, it deletes all spaces from the left.

```python
text = "   example   "
cleaned_text = text.lstrip()
print(cleaned_text)  # Output: "example   "
```

If you enter an argument into `lstrip()`, the character you enter will be removed from the left side (the beginning) of the string:

```python
text = "+++example+++"
cleaned_text = text.lstrip("+")
print(cleaned_text)  # Output: "example+++"
```

In the above example,`lstrip("+")` removes all the plus signs from the left side of the string.

### `rstrip()`

The `rstrip()` method is another way to trim strings in Python. It removes spaces or other characters from the **end of a string** (the right side). It goes through the string from right to left and removes all the relevant characters until it encounters one that shouldn’t be removed. If you use `rstrip()` without an argument, it will delete all spaces from the end of the string.

```python
text = "   example   "
cleaned_text = text.rstrip()
print(cleaned_text)  # Output: "   example"
```

With an argument, the method will remove the specified characters from the end (right side) of the string.

```python
text = "---example---"
cleaned_text = text.rstrip("-")
print(cleaned_text)  # Output: "---example"
```

In the above example, `text.rstrip("-")` removes the minus signs from the right side of the string.


This is a markdown version of: [https://www.ionos.com/en-ie/digitalguide/websites/web-development/python-trim-functions/](https://www.ionos.com/en-ie/digitalguide/websites/web-development/python-trim-functions/) for AI/LLM consumption.