Python String lstrip() Method

04-01-23 Ahmed Obaid 1106 0

The lstrip() function returns a copy of the string with the leading characters removed (depending on the passed character argument). If no argument is passed, it removes the leading blank spaces from the left.


How to formulate it like this:


string. lstrip(characters)

characters: [Optional] A set of characters to remove as leading characters. By default, it removes leading blank spaces.

Return value: Returns a new string with the leading characters removed.


Example:


string = " learn python with ahmed obaid"
# Removes spaces from left.
print(string.lstrip())

The output will be:


learn python with ahmed obaid

In the following example we will select one or more characters to remove from the string in any order.

Example:


string = "**z*x..N.+!!+* learn python with ahmed obaid"
# Removes given set of characters from left.
print(string.lstrip("+.!*xNz")) # remove +.!*xNz

The output will be:


learn python with ahmed obaid

Example:


string = "https://ahmedobaid.com"
# Removes given set of characters from left.
print(string.lstrip("https://")) # remove https://

The output will be:


ahmedobaid.com

 External sources:

 Built-in functions - official Python documentation

 If you have any questions or concerns, leave them in the comments



Tags


Python Python String string methods python python string lstrip method

Share page