Common Python string operations

Source: Internet
Author: User

Python string operations (string replacement, deletion, interception, replication, connection, comparison, search, inclusion, case-insensitive conversion, and Segmentation)

Python string operations (string replacement, deletion, truncation, replication, connection, comparison, search, inclusion, case-insensitive conversion, segmentation, etc.) Remove spaces and special symbols s. strip (). lstrip (). rstrip (',') Copy string # strcpy (sStr1, sStr) sStr = 'strcpy' print sStr connection string # strcat (sStr1, sStr) sStr = 'strcat' sStr = 'append' sStr + = sStr print sStr search character # strchr (sStr1, sStr) sStr = 'strchr' sStr = 's' nPos = sStr1.index (sStr) print nPos comparison string # strcmp (sStr1, sStr) sStr = 'strchr' sStr = 'strch' print cmp (sStr1, sStr) scan string to see if it contains specified characters # strspns (sStr1, sStr) sStr = '000000' sStr = '000000' # sStrand chars both in sStrand sStr print len (sStrand sStr) String Length # strlen (sStr1) sStr = 'strlen' print len (sStr1) convert the case in the string # strlwr (sStr1) sStr = 'jcstrlwr 'sStr = sStr1.upper () # sStr = sStr1.lower () print sStr append the string of the specified length # strncat (sStr1, sStr, n) sStr = '123' sStr = 'abcdef 'n = 3 sStr + = sStr [0: n] print sStr string specified length comparison # strncmp (sStr1, sStr, n) sStr = '000000' sStr = '13bc' n = 3 print cmp (sStr1 [0: n], sStr [0: n]) copy characters of the specified length # strncpy (sStr1, sStr, n) sStr = ''sstr = '000000' n = 3 sStr = sStr [0: n] print sStr Replace the first n characters of the string with the specified character # strnset (sStr1, ch, n) sStr = '000000' ch = 'R' n = 3 sStr = n * ch + sStr1 [3:] print sStr scan string # strpbrk (sStr1, sStr) sStr = 'cekjgdklab 'sStr = 'gka' nPos =-1 for c in sStr1: if c in sStr: nPos = sStr1.index (c) break print nPos flip string # strrev (sStr1) sStr = 'abcdefg' sStr = sStr1 [:-1] print sStr search string # strstr (sStr1, sStr) sStr = 'abcdef' sStr = 'cde' print sStr1.find (sStr) split string # strtok (sStr1, sStr) sStr = 'AB, cde, fgh, ijk 'sstr =', 'sstr = sStr1 [sStr1.find (sStr) + 1:] print sStr or s = 'AB, cde, fgh, ijk' print (s. split (',') connection string delimiter = ', 'mylist = ['Brazil', 'Russia ', 'India', 'China'] print delimiter. implementation of addslashes in join (mylist) PHP def addslashes (s): d = {'"':'\\"',"'":"\\'", "\ 0": "\ 0", "\": "\"} return ''. join (d. get (c, c) for c in s) s = "John 'Johnny 'Doe (a.k. a. \ "Super Joe \") \ 0 "print s print addslashes (s) only show letters and numbers def OnlyCharNum (s, oth =''): s = s. lower (); fomart = 'abcdefghijklmnopqrstuvwxyz013456789 'for c in s: if not c in fomart: s = s. replace (c, ''); return s; print (OnlyStr (" a000 aa-B "))

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.