Python String lstrip() Method Examples

In the previous article, we have discussed Python Program for ljust() Function
lstrip() Function in Python:

Any leading characters are removed using the lstrip() method (space is the default leading character to remove)

Syntax:

string.lstrip(characters)

Parameters

characters: This parameter is optional, and omitting it causes the lstrip function to treat white spaces as the default parameter. In Python, specify the Characters to strip from the left-hand side of a string to change the default white space.

Examples:

Example1:

Input:

Given first String =  "####python###"
Given character = '#'

Output:

The above given first string is : ####python###
The given first string after applying lstrip() function: python###

Example2:

Input:

Given second String = "@@@@@###_GOODMORNING_22222"
Given characters = "@,#"

Output:

The above given second string is : @@@@@###_GOODMORNING_22222
The given second string after applying lstrip() function: _GOODMORNING_22222

Program for lstrip() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first string as static input and store it in a variable.
  • Give the character as static input and store it in another variable.
  • Apply lstrip() method to the given string for the given character in which any leading characters are removed using the lstrip() method (space is the default leading character to remove).
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying the lstrip() function.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "####python###"
# Give the character as static input and store it in another variable.
gvn_chactr = '#'
# Apply lstrip() method to the given string for the given character in which
# any leading characters are removed using the lstrip() method
# (space is the default leading character to remove).
# Store it in another variable.
rslt_str1 = gvn_fststr.lstrip(gvn_chactr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying lstrip() function.
print("The given first string after applying lstrip() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = "@@@@@###_GOODMORNING_22222"
rslt_str2 = gvn_scndstr.lstrip()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying lstrip() function:", rslt_str2)

Output:

The above given first string is : ####python###
The given first string after applying lstrip() function: python###
The above given second string is : @@@@@###_GOODMORNING_22222
The given second string after applying lstrip() function: @@@@@###_GOODMORNING_22222

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first string as user input using the input() function and store it in a variable.
  • Give the character as user input using the input() function and store it in another variable.
  • Apply lstrip() method to the given string for the given character in which any leading characters are removed using the lstrip() method (space is the default leading character to remove).
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying the lstrip() function.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as user input using the input() function and store it in a variable.
gvn_fststr =  input("Enter some random string = ")
# Give the character as user input using the input() function and store it in another variable.
gvn_chactr = input("Enter some random character = ")
# Apply lstrip() method to the given string for the given character in which
# any leading characters are removed using the lstrip() method
# (space is the default leading character to remove).
# Store it in another variable.
rslt_str1 = gvn_fststr.lstrip(gvn_chactr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying lstrip() function.
print("The given first string after applying lstrip() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some random string = ")
gvn_chactr2 = input("Enter some random character = ")
rslt_str2 = gvn_scndstr.lstrip(gvn_chactr2)
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying lstrip() function:", rslt_str2)

Output:

Enter some random string = hello all
Enter some random character = ^
The above given first string is : hello all
The given first string after applying lstrip() function: hello all
Enter some random string = 555555python_platform_4545
Enter some random character = 5
The above given second string is : 555555python_platform_4545
The given second string after applying lstrip() function: python_platform_4545

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.