If you have a Python text string, and want trailing spaces removed you can do that with the method
That removes the spaces before and after the string.
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' >>> >>>
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' >>>
Write 100% better code with 250k+ posts
Level up every day