How to Remove Leading Whitespaces at Start of a String in Python
To remove all leading whitespaces in a string in Python without modifying any other whitespaces, use the Python .lstrip()
function. Pass the string to modify before the function with a .
(dot) separator and store the result in a variable.
Let's demonstrate this on a string where there are unwanted spaces at the beginning but a required newline character at the end.
text = ' Some text\n'
result = text.lstrip()
print(result)
Some text