> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Use String Remove Affix

#### Sourcery refactoring id: `use-string-remove-affix`

#### Description:

Replaces string slicing with the Python 3.9 features `removesuffix` and
`removeprefix`.

#### Before:

```python
if text.startswith("Hello, "):
    text = text[7:]

if text.endswith(" World!"):
    text = text[: -len(" World!")]
```

#### After:

```python
text = text.removeprefix("Hello, ")
text = text.removesuffix(" World!")
```

#### Explanation:

This refactoring is motivated by
[PEP 616](https://www.python.org/dev/peps/pep-0616/). As stated in the
[documentation](https://www.python.org/dev/peps/pep-0616/#id18), using these
methods confers several advantages, including being more robust to user error,
more performant, and more descriptive.
