Python (5) string processing (Sub,replace,find,index,upper,strip)

Source: Internet
Author: User

One, the use of sub and replace

The Re.sub function takes a regular expression-based substitution work

Re.sub Replace with a, B or C in the target string and replace all

1>>>ImportRe2>>> Re.sub ('[ABC]','o','Mark')3 'Mork'4>>> Re.sub ('[ABC]','o','caps')5 'oops'6>>

Replace usage Description:

1>>>a2 'asds23dfg34'3>>> A.replace ('s','M')#Replace All4 'amdm23dfg34'5>>> B ='Adfafafafa'6>>> B.replace ('a','M', 3)#the substitution of the specified number7 'Mdfmfmfafa'

Second, the use of Find and Index

Index,find returns the subscript of the found string; If the returned value is not found, then-1, and index throws an exception directly

A.find (' t ', start) search from start position

A.find (' t ', start,end) search from the specified position

A.rfind (' t ') start search from the right position

A.count (' t ') how many specified characters are searched

1>>> A ='12432423'2>>> A.find ('1')3 04>>> A.find (5)5 Traceback (most recent):6File"<stdin>", Line 1,inch<module>7 typeerror:expected A character buffer object8>>> A.find (' -')91
1>>>a2 '12432423'3>>> f = a.find ('M')4>>>F5-16>>> f = a.find (' +')#returns the first position of a string7>>>F829>>> f = a.find ('435')Ten>>>F One-1
1 '12432423'2>>> A.index ('4')324>>> A.index ('8')5 Traceback (most recent):6File"<stdin>", Line 1,inch<module>7Valueerror:substring notfound8>>> f = a.index ('8')9 Traceback (most recent):TenFile"<stdin>", Line 1,inch<module> OneValueerror:substring notFound

Three, case processing, upper,lower,swapcase,capitalize,title

1>>>a2 '123dfsdfs'3>>> A ='asds23dfg34'4>>> A.upper ()#ALL caps5 'asds23dfg34'6>>> A.lower ()#All lowercase7 'asds23dfg34'8>>> A.swapcase ()#Case Exchange9 'asds23dfg34'Ten>>> a.capitalize ()#initials uppercase remaining lowercase One 'asds23dfg34' A>>>A.title () - 'asds23dfg34'          #substring, first letter uppercase ->>> the Keyboardinterrupt

Four, the use of strip

The strip in Python is used to remove the end and end characters of a string, so Lstrip is used to remove the left character, Rstrip to remove the right character.

Each of these three functions can pass in a parameter that specifies the first and last characters to be removed.

It is important to note that a character array is passed in , and the compiler strips out all the corresponding characters at both ends

1>>>a2 'asds23dfg34'3>>> A.strip ('a')4 'sds23dfg34'5>>> A.strip ('s')6 'asds23dfg34'7>>> s ='saaaay Yes No yaaaass'8>>> S.strip ('say')#Find them on both sides .9 'Yes No'                   #There's a space on each side .Ten>>> S.lstrip ('say')#just look to the left. One 'Yes No Yaaaass'         A>>> S.rstrip ('say')#just find the right . - 'saaaay Yes No'

Python (5) string processing (Sub,replace,find,index,upper,strip)

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.