Python String lstrip()

Python String lstrip() method

Python String lstrip() method returns a string with the whitespaces or specified characters removed from the starting/left-side of given string.

Consider that given string is in x.

x = "  hello world    "

Then the return value of x.lstrip() is

"hello world    "

In this tutorial, you will learn the syntax and usage of String lstrip() method in Python language.

Syntax of lstrip() method

The syntax of String lstrip() method in Python is given below.

str.lstrip(chars)

Parameters

The string lstrip() method takes a single parameter.

ParameterDescription
charsOptional
A string of characters.
These specified characters are removed from the left side of the string.
Default value is None.
Python String lstrip() parameters

Return value

The string lstrip() method returns a String value.

Examples

In the following examples, you will learn how to use lstrip() method to strip whitespaces from the left side of the string, and how to use lstrip() method to strip specified characters from the left side of the string.

1. Strip whitespaces from left side of string in Python

In this example, we take a string value in variable x, where string contains some whitespaces at the beginning and ending of the string. We have to remove the whitespaces present at the left edge of the string.

Steps

  1. Given a string in x.
  2. Call lstrip() method on the string x. By default, when no argument is specified, the lstrip() method removes whitespaces characters from the left side edge of the string.
x.lstrip()
  1. The lstrip() method returns a new string, created from removing the whitespace characters from the left side of the original string. Assign the returned value to a variable, say x_stripped.
x_stripped = x.lstrip()
  1. You may print the stripped string to output.

Program

The complete program to strip whitespace characters from the left side of given string.

Python Program

# Given string
x = "  hello world    "

# Strip whitespaces on left side of string
x_stripped = x.lstrip()

print(f"Original      : \"{x}\"")
print(f"Left Stripped : \"{x_stripped}\"")
Run Code Copy

Output

Original      : "  hello world    "
Left Stripped : "hello world    "

Please note that the whitespace characters which are not at the edges remain in the returned string, like the single space between hello and world.

2. Strip specified characters from left side of the string in Python

Instead of whitespace characters, we can also specify characters via argument to lstrip() method, to strip from the left side of the string.

In this example, we take a string value in variable x, and we have to remove the characters ".-," [dot, hyphen, comma] present at the left side or starting of the string.

Steps

  1. Given a string in x.
  2. Call lstrip() method on the string x and pass ".-," for the argument to the lstrip() method.
x.lstrip(".-,")
  1. The lstrip() method returns a new string, created from removing the specified characters, which are dot, hyphen, and comma, from the starting of the string. Assign the returned value to a variable, say x_stripped.
x_stripped = x.lstrip()
  1. You may print the stripped string to output.

Program

The complete program to strip specified characters from the left side of given string.

Python Program

# Given string
x = "......,,hello world----...-"

# Strip whitespaces on left side of string
x_stripped = x.lstrip(".-,")

print(f"Original      : \"{x}\"")
print(f"Left Stripped : \"{x_stripped}\"")
Run Code Copy

Output

Original      : "......,,hello world----...-"
Left Stripped : "hello world----...-"

Summary

In this tutorial of Python String Methods, we learned about String lstrip() method, its syntax, and examples.

Related Tutorials

Code copied to clipboard successfully 👍