Skip to content

Python lstrip Function | trim string from left examples

  • by

Python lstrip() method is used to remove all leading White-space (characters) from the string. There is an option in “lstrip Function” to use or not a char type parameter. If the parameter is not provided, it removes all the leading spaces from the string.

Read more topic & links

Syntax

lstrip([chars]) 

Parameters

chars (optional): A list of chars to be removed.

Note: If the chars parameter is not provided, all leading whitespaces are removed from the string.

Return Value

Returns a copy of the string with leading (left) characters stripped.

lstrip() | Python trim string from left example

1. Removing left side white space from strings example

str1 = "  EyeHunts  "

print(str1.lstrip())

Output: EyeHunts

2. Remove special char- Python string lstrip

# Given string
string = "***## EyeHunts"

# Removes given set of characters from left.
print(string.lstrip("*#"))

Output: EyeHunts

3. Remove substring from left

Will remove only the first occurrence of a substring from the given string.

string = "The The Python code "

# Argument doesn't contain leading 'g'  
# So, no characters are removed  
print(string.lstrip('The'))

Output: The Python code

Runtime Error | lstrip()

Will throw an error, if you try to strip anything except a string. In the example of trying to strip a Python List.

list1 = [1, 2, 3]

print(list.lstrip())

Output: AttributeError: type object ‘list’ has no attribute ‘lstrip’

Python lstrip Function trim string from left examples

Q: How to trim a string from left in python?

Answer: Use lstrip() python function to remove(trim) left space from given string.

string.lstrip()

Do comment if you have any questions and doubts on this topic.

Note:
IDE: PyCharm 2020.1 (Community Edition)
macOS 10.15.4
Python 3.7
All Python Examples string.count python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading