What is the return value of the string method lstrip()?

Examples

Basic usage

To strip all leading whitespace from " Dog":

x = " Dog"

x.lstrip[]

'Dog'

Chars parameter

To remove all combinations of leading 'k S' from " SkyTowner":

y = " SkyTowner"

y.lstrip['k S']

'yTowner'

We remove all combinations of 'k', ' ' and 'S' from the beginning of the string which gives us return value of 'yTowner'.

Case sensitivity

To remove all leading 's' from "SkyTowner":

z = "SkyTowner"

z.lstrip['s']

'SkyTowner'

Note that no leading characters are removed as there is a mismatch at the very beginning of the string where char argument of 's' does not match with 'S'. As this shows, lstrip[~] method is case sensitive.

Python provides three methods that can be used to trim whitespaces from the string object.

Python Trim String

  • strip[]: returns a new string after removing any leading and trailing whitespaces including tabs [\t].
  • rstrip[]: returns a new string with trailing whitespace removed. It’s easier to remember as removing white spaces from “right” side of the string.
  • lstrip[]: returns a new string with leading whitespace removed, or removing whitespaces from the “left” side of the string.

All of these methods don’t accept any arguments to remove whitespaces. If a character argument is provided, then they will remove that characters from the string from leading and trailing places. Let’s look at a simple example of trimming whitespaces from the string in Python.

s1 = ' abc ' print[f'String =\'{s1}\''] print[f'After Removing Leading Whitespaces String =\'{s1.lstrip[]}\''] print[f'After Removing Trailing Whitespaces String =\'{s1.rstrip[]}\''] print[f'After Trimming Whitespaces String =\'{s1.strip[]}\'']

Output:

String =' abc ' After Removing Leading Whitespaces String ='abc ' After Removing Trailing Whitespaces String =' abc' After Trimming Whitespaces String ='abc'

Let’s look at some more examples with strings having a new-line and tabs.

>>> s1 = ' X\n Y \nZ \t' >>> s1.strip[] 'X\n Y \nZ' >>> s1.rstrip[] ' X\n Y \nZ' >>> s1.lstrip[] 'X\n Y \nZ \t'

You can check out more Python string examples from our GitHub Repository.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up

The lstrip[] method returns a copy of the string by removing leading characters specified as an argument. By default, it removes leading whitespaces if no argument passed.

Leading characters occur at the start of the string [leftmost part of the string].

Syntax:

str.lstrip[characters]

Parameters:

characters: [optional] A char or string to be removed from the starting of the string.

Return Value:

A string.

The following examples demonstrates the lstrip[] method.

>>> mystr = ' Hello World ' >>> mystr.lstrip[] # removes leading spaces 'Hello World ' >>> mystr = ''' Python is a programming language''' >>> mystr.lstrip[] # removes leading \n 'Python is \na programming language' >>> mystr = '----Hello World----' >>> mystr.lstrip['-'] # removes leading - 'Hello World----'

You can specify one or more characters as a string to be removed from the string in any order, as shown below.

>>> '#$2Hello World#$2'.lstrip['$2#'] 'Hello World#$2' >>> '#$-2Hello World#$2'.lstrip['$2#'] '-2Hello World#$2' >>> 'www.tutorialsteacher.com'.lstrip['xabw.'] # remove www. 'tutorialsteacher.com' >>> 'ābc'.lstrip['ā'] # removes Unicode char 'bc'

In the above example, '#$2Hello World#$2'.lstrip['$2#'] removes leading chars'$', '2', or '#' if appears in any order. However, #$-2Hello World#$2'.lstrip['$2#'] removes only leading '#' and '$', because '-' is not specified to be removed, so it stops there and consider it as a word.

Want to check how much you know Python?

What is Lstrip [] function in string?

The lstrip[] method returns a copy of the string with leading characters removed [based on the string argument passed]. The lstrip[] removes characters from the left based on the argument [a string specifying the set of characters to be removed]. The syntax of lstrip[] is: string.lstrip[[chars]]

What is the return value of the string method Istrip []?

Python sting istrip[] method It is used used to remove the white space from the start and end of the string and returns the original string without white space.

How do you use Lstrip and Rstrip in Python?

Python Trim String.
strip[]: returns a new string after removing any leading and trailing whitespaces including tabs [ \t ]..
rstrip[]: returns a new string with trailing whitespace removed. ... .
lstrip[]: returns a new string with leading whitespace removed, or removing whitespaces from the “left” side of the string..

What character will Lstrip remove from the beginning of a string?

To remove only leading whitespace and characters, use . lstrip[] . This is helpful when you want to remove whitespace and characters only from the start of the string. An example for this would be removing the www.

Chủ Đề