An Interest In:
Web News this Week
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
March 4, 2020 05:05 pm GMT
Original Link: https://dev.to/libertycodervice/remove-trailing-spaces-2m5
remove trailing spaces
If you have a Python text string, and want trailing spaces removed you can do that with the method .strip()
, .lstrip()
and .rstrip()
.
That removes the spaces before and after the string.
Example
You can use strip, lstrip, rstrip methods. Like the name suggests, lstrip
strips left of the string, rstrip
strips right of the string and strip
just strips the string of spaces.
First create a string surrounded by spaces:
>>> a = "abc".center(30)>>> a' abc '>>>
To do a left strip, call the method lstrip()
. This removes spaces to the left of the string:
>>> b = a.lstrip()>>> b'abc '
The results of a right strip, call rstrip()
which removes spaces to the right of the string:
>>> >>> c = a.rstrip()>>> c' abc'
To do a general strip, that removes all spaces on both sides:
>>> >>> d = a.strip()>>> d'abc'>>> >>>
The strip()
function only does trailing spaces, not spaces inside the string itself.
>>> a = " aaaa bbbb ccc dddd eee ">>> a.strip()'aaaa bbbb ccc dddd eee'>>>
Original Link: https://dev.to/libertycodervice/remove-trailing-spaces-2m5
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To