The strip and split functions of Python

Source: Internet
Author: User
Tags function prototype

First,strip function prototype

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

as follows :

>>> a= ' hheloooo goooodbyyyye ' >>> a.strip (' helo ') ' goooodbyyyy ' >>> a.strip (' he ') ' loooo Goooodbyyyy ' >>> a.strip (' o ') ' hheloooo Goooodbyyyye ' >>>

start looking for . first find ' h ' ' h ' minus , found the second ' H ' discover ' E ' [' H ', ' e ', ' l ', ' O '] inside , continue to remove ' e ',

from the tail start to find ' e ' in [' H ', ' e ', ' l ', ' O '], remove ' e ', and then find ' y ' not in [' H ', ' e ', ' l ' , ' O ') inside , so it stopped .

1, when rm is empty, the default is to remove the white space character (including ' \ n ', ' \ R ', ' \ t ', ')

>>> a= ' A\N\TBC ' >>> print a A bc>>> a.strip () ' A\N\TBC ' >>> a= ' abc ' >> ;> A.strip () ' abc ' >>> a= ' \n\tabc ' >>> a.strip () ' abc ' >>> a= ' abc\n\t ' >>> a.strip () ' ABC ' >>>

2, where the rm Delete sequence is deleted as long as the characters on the edge (beginning or end) are deleted in the sequence

>>> a= ' 123abc ' >>> a.strip (' + ') ' 3abc ' >>> a.strip (' + ') ' 3abc ' >>> a.strip (' 1a ') ' 23abc ' >>> A.strip (CB) Traceback (most recent): File "<stdin>", line 1, in <module>nameerror : Name ' CB ' is not defined>>> a.strip (' CB ') ' 123a ' >>> a.strip (' BC ') ' 123a ' >>>

second,split function

Split is a split function that splits a string into a "character" and is saved in a list.

>>> A= ' a b c d ' >>> a.split () [' A ', ' B ', ' C ', ' d ']

The default is a space split with no parameters. The reason for the "character" of double quotes, because the actual Python does not have characters.

>>> b= ' abc efg hij kkj ' >>> b.split () [' abc ', ' EFG ', ' hij ', ' kkj ']

can also be split with parameters according to actual requirements

>>> c= ' name=ding|age=25|job=it ' >>> c.split (' | ') [' name=ding ', ' age=25 ', ' Job=it ']>>> c.split (' | ') [0].split (' = ') [' name ', ' Ding ']

You can also take a number parameter, which means "cut a few knives" such as:

>>> d= ' A b c d e ' >>> d.split (', 1) #以空格 "Cut a knife", divided into two pieces [' A ', ' b c d e ']>>> d.split (', 2 ') [' A ', ' B ', ' C d e ']>>> d.split (' A ', ' 3 ') [' A ', ' B ', ' C ', ' d e ']>>> d.split (', ', -1 ') #d. Split (') as a result [' A ', ' B ', ' C ', ' d ', ' E ']>>> d.split (') [' A ', ' B ', ' C ', ' d ', ' e ']


This article is from the "Ding classmate 1990" blog, please be sure to keep this source http://dingtongxue1990.blog.51cto.com/4959501/1675499

The strip and split functions of 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.