How to remove all trailing and leading whitespace in a string in Python?

PythonServer Side ProgrammingProgramming

To remove all trailing and leading whitespace in a string, we can use the method strip() in String class that gets rid of both these whitespaces. You can use it as follows:

>>> '   Hello People   '.strip()
'Hello People'

If you only want to remove leading or trailing whitespace use lstrip() or rstrip() respectively.

>>> '   Hello People'.lstrip()
'Hello People'
>>> 'Hello People   '.rstrip()
'Hello People'
raja
Updated on 30-Sep-2019 06:38:11

Advertisements