YouTip LogoYouTip

Att String Lstrip

## Python lstrip() Method The `lstrip()` method is a built-in Python string method used to remove leading characters (characters on the left side) from a string. By default, it removes whitespace, but it can also be configured to strip a specific set of characters. --- ## Description The `lstrip()` method returns a copy of the string with leading characters removed. * If no argument is passed, it removes all leading whitespace characters (spaces, tabs, newlines). * If a character set is specified, it removes all combinations of those characters from the left side of the string until it encounters a character that is not in the specified set. --- ## Syntax ```python str.lstrip() ``` ### Parameters * **`chars`** *(optional)*: A string specifying the set of characters to be removed. If omitted or `None`, the method defaults to removing whitespace characters. ### Return Value * Returns a **new string** with the leading characters removed. The original string remains unmodified because Python strings are immutable. --- ## Code Examples ### Example 1: Removing Leading Whitespace (Default Behavior) When no arguments are provided, `lstrip()` removes spaces, tabs (`\t`), and newline characters (`\n`) from the left side of the string. ```python # String with leading and trailing spaces text = " this is string example....wow!!! " # Remove leading spaces print(text.lstrip()) ``` **Output:** ```text this is string example....wow!!! ``` --- ### Example 2: Removing Specific Leading Characters You can pass a string of characters to the `chars` parameter. Python will strip any character in that set from the left side of the string. ```python # String with leading and trailing '8's text = "88888888this is string example....wow!!!8888888" # Remove leading '8's print(text.lstrip('8')) ``` **Output:** ```text this is string example....wow!!!8888888 ``` --- ### Example 3: Stripping Multiple Characters The `chars` argument is treated as a **set of characters**, not a prefix substring. The method will strip any character in the set until it hits a character that is not in the set. ```python # String with various leading characters text = "www.example.com" # Strip 'w', '.', and 'e' from the left print(text.lstrip("w.e")) ``` **Output:** ```text xample.com ``` *Explanation: The method stripped all 'w's, the '.', and the 'e' because they were all specified in the `chars` argument `"w.e"`. It stopped at 'x' because 'x' is not in the set.* --- ## Considerations & Best Practices 1. **Immutability:** Like all Python string methods, `lstrip()` does not modify the original string. It returns a new string. If you need to save the result, reassign it to a variable: ```python text = text.lstrip() ``` 2. **Character Set Matching:** The `chars` argument is not a substring pattern. For example, `text.lstrip("sp")` will remove all leading `'s'` and `'p'` characters individually, regardless of their order, not just the exact sequence `"sp"`. 3. **Alternative Methods:** * Use `rstrip()` to remove trailing characters from the right side. * Use `strip()` to remove characters from both the left and right sides. * In Python 3.9+, if you want to remove an exact prefix substring rather than a set of characters, use `removeprefix()` instead.
← Att String MaketransAtt String Lower β†’