Python trim functions can be used to remove un­nec­es­sary char­ac­ters and white­space, making them useful functions for cleaning up input and nor­mal­iz­ing strings.

Web Hosting
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support

Why trim strings in Python?

In Python, you should modify strings to ensure clean data pro­cess­ing and display. This will increase read­abil­i­ty and improve the ap­pear­ance of user in­ter­faces and outputs. Trimming is es­pe­cial­ly useful when it comes to deleting un­in­tend­ed white­space in user entries and working with external data sources like files and APIs.

You can also use Python trim functions for data val­i­da­tion. This ensures that all strings conform to the expected Python string format and your data is con­sis­tent. However, keep in mind that trimming strings might result in the loss of relevant in­for­ma­tion. It’s important to take care when using these functions and only use them in the right sit­u­a­tions.

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

Trimming is the process of removing certain char­ac­ters 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 white­space and other char­ac­ters from the beginning and end of a string. It’s best used to tidy up strings and rid them of un­nec­es­sary char­ac­ters 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 char­ac­ters 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 char­ac­ters 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 char­ac­ters from the end of a string (the right side). It goes through the string from right to left and removes all the relevant char­ac­ters until it en­coun­ters 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 char­ac­ters 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.

Go to Main Menu