A summary of the usage of the Python strip () function and the Split function

Source: Internet
Author: User

Strip function prototypes

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

Note :

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

For example:

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

For example:

Example:

Split usage of strings

Description
There is no character type in Python, only a string, the character here is a string that contains only one character!!!

Split returns a list.

1. Divide by a character, such as '. '

1234 str= (‘www.google.com‘)print strstr_split = str.split(‘.‘)printstr_split

The results are as follows:



2. Divide by one character and divide n times. If you press '. ' Split 1 times


?
1234 str= (‘www.google.com‘)print strstr_split = str.split(‘.‘1)printstr_split

The results are as follows:


3. Split by a string. such as: ' | | '


?
1234 str= (‘WinXP||Win7||Win8||Win8.1‘)print strstr_split = str.split(‘||‘)printstr_split

The results are as follows:


4. Divide by a string and divide n times. such as: Press ' | | ' Split 2 times


?
1234 str= (‘WinXP||Win7||Win8||Win8.1‘)print strstr_split = str.split(‘||‘,2)printstr_split

The results are as follows:


5. Divide by a character (or string), divide n times, and assign the completed string (or character) of the partition to a new (n+1) variable. (Note: See opening instructions)
such as: Press '. ' Splits the character, splits it 1 times, and assigns the split string to 2 variables Str1,str2


?
1234 url =(‘www.google.com‘)str1, str2 = url.split(‘.‘, 1)print str1printstr2

The results are as follows:


A summary of the usage of the Python strip () function and the Split function

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.