Strip (), Lstrip (), Rstrip () usage in Python

Source: Internet
Author: User

In Python, there are three functions that remove the kinsoku, whitespace characters, which are:
Strip: Used to remove Kinsoku, whitespace characters (including \ n, \ r, \ t, "', i.e.: newline, carriage return, tab, space)
Lstrip: Used to remove opening characters, white space characters (including \ n, \ r, \ t, "', i.e.: newline, carriage return, tab, space)
Rstrip: Used to remove trailing characters, whitespace (including \ n, \ r, \ t, ' ', i.e.: newline, carriage return, tab, space)

Note: These functions will only delete the header and tail characters, and the middle one will not be deleted.

The usage is:
String.strip ([chars])
String.lstrip ([chars])
String.rstrip ([chars])

The parameter chars is optional, and when chars is empty, the whitespace characters (including \ n, \ r, \ T, ") are removed by default for the string tail
When chars is not empty, the function is chars into characters and then the characters are removed.

It returns a string copy that removes the kinsoku (or whitespace) characters, and the string itself does not change.

Examples are as follows:
1. When chars is empty, the default is to remove the white space character (including ' \ n ', ' \ R ', ' \ t ', ')

 >>> str =  "  AB CD  "  >>> str    AB CD   " >>> Str.strip () Span style= "COLOR: #008000" >#   Remove the Kinsoku space   ab cd   " >>> Str.lstrip () Span style= "COLOR: #008000" >#   remove opening spaces   ab cd   " >>> Str.rstrip () Span style= "COLOR: #008000" >#   delete trailing spaces    AB CD   " 

2. When chars is not empty, the function is chars into characters and then the characters are removed.

>>> str2 ='1a2b12c21'>>> Str2.strip (' A')#Remove 1 and 2 from the tail'a2b12c'>>> Str2.lstrip (' A')#Delete 1 and 2 at the beginning'A2b12c21'>>> Str2.rstrip (' A')#Delete End of 1 and 2'1a2b12c'

Strip (), Lstrip (), Rstrip () usage in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.