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 normalizing strings.

Web Hosting
Fast, scalable hosting for any website
  • 99.9% uptime
  • PHP 8.3 with JIT compiler
  • SSL, DDoS protection, and backups

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:

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

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

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

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:

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

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.

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

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

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

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.

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

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

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

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

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top