How to get started with Python basics how to use the Strip () function to go to a space \n\r\t

Source: Internet
Author: User
python3.4 Study notes (20) Python strip () function de-whitespace \n\r\t function usage

In Python, there are three functions in the string handler function that go to whitespace (including ' \ n ', ' \ R ', ' \ t ', '):

Strip at the same time remove both left and right spaces
Lstrip Remove the left space
Rstrip Remove the right space

Specific examples are as follows:

>>>a= "Gho stwwl" >>>a.lstrip () ' Gho stwwl ' >>>a.rstrip () ' Gho stwwl ' >>>a.strip () ' Gho STWWL '

Declaration: S is a string, RM is a sequence of characters to be deleted
S.strip (RM) Remove the characters from the beginning and end of the s string in the RM delete sequence
S.lstrip (RM) Delete the character in the S string at the beginning of the RM delete sequence
S.rstrip (RM) Removes the character from the end of the S string that is located in the RM delete sequence

Attention:
1. When RM is empty, the default is to remove the whitespace characters (including ' \ n ', ' \ R ', ' \ t ', ')

>>> a = ' 123 ' >>> a.strip () ' 123 ' >>> a= ' \t\tabc ' abc ' >>> a = ' sdff\r\n ' >>> a . Strip () ' SDFF '

2. The RM delete sequence here is deleted as long as the character on the edge (beginning or end) is within the delete sequence.

>>> a = ' 123abc ' >>> a.strip (' + ') ' 3ABC ' results are the same >>> a.strip (' + ') ' 3ABC '

"Recommended"

1. Python Free video tutorial

2. How to use Strip () and split () in Python

3. Detailed usage scenarios for strip functions in Python

4. Python strip () little-known traps

5. The magic of the Strip () function that you do not know 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.