Computer >> Computer tutorials >  >> Programming >> Python

How to remove all leading whitespace in string in Python?


The lstrip() method will remove leading whitespaces, newline and tab characters on a string beginning. You can use it in the following way:

>>> '     hello world!'.lstrip()
'hello world!'

You can also use the strip() function to remove both trailing and leading whitespace in the same manner. For example:

>>> '     hello world!    '.strip()
'hello world!'