Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 4, 2020 05:05 pm GMT

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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To