Python Trim: Remove Whitespaces From Strings

In this short tutorial, we look at how to use Python to trim a string. We also look at all the built-in methods to remove whitespaces in a string.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often us…


This content originally appeared on DEV Community and was authored by hrishikesh1990

In this short tutorial, we look at how to use Python to trim a string. We also look at all the built-in methods to remove whitespaces in a string.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Contents - Python Trim

Why do we use Python to trim whitespaces?

Python trim essentially means removing whitespaces from a string. While dealing with strings in files or user input strings, whitespaces are a common problem. Since Python considers whitespaces as a character, they would also be printed in case you decide to print the string. In some cases, it could break the logic of your code as well.

So it is best to check for and remove whitespaces in strings and Python comes with 3 inbuilt methods to remove whitespaces.

3 methods to remove whitespaces:

Since all the methods have the same syntax I have explained the concepts first and added a code snipped of all the methods in the end.

strip():

The strip() method is the most commonly accepted method to remove whitespaces in Python. It is a Python built-in function that trims a string by removing all leading and trailing whitespaces.

strip() syntax is as follows:

string.strip(characters)

Here, “string” refers to the string containing the whitespaces

Parameters:

  • Characters - Optional, a set of characters that you want to remove. if left empty whitespaces would be removed

lstrip():

lstrip() in Python is used to trim the string from the left. This means that all whitespaces from the left side of the string will be removed.

The syntax and parameters are the same as strip()

rstrip():

rstrip() does the opposite of lstrip(). It removes all the whitespaces from the right side of the string.

The syntax and parameters are again similar to strip()

Code and Explanation:

str_1 = "  Hire freelance developers  "

print(str_1.strip())
#Output = "Hire freelance developers"

print(str_1.rstrip())
#Output = "  Hire freelance developers"

print(str_1.lstrip())
#Output = "Hire freelance developers  "

As you can see we have used strip() first and all the leading and trailing whitespaces were removed.

In the following output, the trailing and leading whitespace have been removed as we have used rstrip() and lstrip() respectively.

Closing thoughts - Python Trim

Like most others, I would also recommend using strip() as it is hard to judge if the string would contain leading or trailing whitespaces. Hence strip() is the safest bet for performing a Python trim.

However, lstrip() and rstrip() serve very specific use cases and I would recommend practicing them as you might come across a suitable use case.


This content originally appeared on DEV Community and was authored by hrishikesh1990


Print Share Comment Cite Upload Translate Updates
APA

hrishikesh1990 | Sciencx (2021-08-17T10:52:02+00:00) Python Trim: Remove Whitespaces From Strings. Retrieved from https://www.scien.cx/2021/08/17/python-trim-remove-whitespaces-from-strings/

MLA
" » Python Trim: Remove Whitespaces From Strings." hrishikesh1990 | Sciencx - Tuesday August 17, 2021, https://www.scien.cx/2021/08/17/python-trim-remove-whitespaces-from-strings/
HARVARD
hrishikesh1990 | Sciencx Tuesday August 17, 2021 » Python Trim: Remove Whitespaces From Strings., viewed ,<https://www.scien.cx/2021/08/17/python-trim-remove-whitespaces-from-strings/>
VANCOUVER
hrishikesh1990 | Sciencx - » Python Trim: Remove Whitespaces From Strings. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/17/python-trim-remove-whitespaces-from-strings/
CHICAGO
" » Python Trim: Remove Whitespaces From Strings." hrishikesh1990 | Sciencx - Accessed . https://www.scien.cx/2021/08/17/python-trim-remove-whitespaces-from-strings/
IEEE
" » Python Trim: Remove Whitespaces From Strings." hrishikesh1990 | Sciencx [Online]. Available: https://www.scien.cx/2021/08/17/python-trim-remove-whitespaces-from-strings/. [Accessed: ]
rf:citation
» Python Trim: Remove Whitespaces From Strings | hrishikesh1990 | Sciencx | https://www.scien.cx/2021/08/17/python-trim-remove-whitespaces-from-strings/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.