lstrip(): removing spaces and chars from the beginning of a string in Python

Use the lstrip() method to remove spaces from the beginning of a string.

regular_text = "   This is a regular text."

no_space_begin_text = regular_text.lstrip()

print(regular_text)
#'   This is a regular text.'

print(no_space_begin_text)
#'This is a regular text.'

Notice that the original regular_text variable remains unchanged, thus you need to assign the return of the method to a new variable, no_space_begin_text in this case.

Removing Chars

The lstrip() method also accepts specific chars for removal as parameters.

regular_text = "$@G#This is a regular text."

clean_begin_text = regular_text.lstrip("#$@G")

print(regular_text)
#$@G#This is a regular text.

print(clean_begin_text)
#This is a regular text.