Python Common string manipulation

Source: Internet
Author: User

Python string manipulation (string substitution, delete, intercept, copy, join, compare, find, include, Case transform, split, etc.) go to spaces and special symbols S.strip (). Lstrip (). Rstrip (',') to copy the string#strcpy (SSTR1,SSTR)Sstr='strcpy'sStr=sStr sStr='strcpy'   PrintSSTR Connection String#strcat (SSTR1,SSTR)Sstr='strcat'sStr='Append'sStr+=sStrPrintSStr Find characters#STRCHR (SSTR1,SSTR)Sstr='STRCHR'sStr='s'NPos=Sstr1.index (SSTR)PrintNPos Comparing Strings#strcmp (SSTR1,SSTR)Sstr='STRCHR'sStr='Strch'   Printwhether the CMP (SSTR1,SSTR) Scan string contains the specified character#strspn (SSTR1,SSTR)Sstr='1345678'sStr='456'   #Sstrand chars both in Sstrand sStrPrintLen (sstrand sStr) string length#strlen (SSTR1)Sstr='strlen'   PrintLen (SSTR1) Converts the case in a string#strlwr (SSTR1)Sstr='jcstrlwr'sStr=Sstr1.upper ()#sstr= sstr1.lower ()PrintSSTR Append a string of the specified length#strncat (sstr1,sstr,n)Sstr='1345'sStr='abcdef'N= 3sStr+=Sstr[0:n]PrintSStr string specifying length comparison#strncmp (sstr1,sstr,n)Sstr='1345'sStr='13BC'N= 3PrintCMP (Sstr1[0:n],sstr[0:n]) copies characters of specified length#strncpy (sstr1,sstr,n)Sstr="'sStr='1345'N= 3sStr=Sstr[0:n]PrintSSTR replaces the first n characters of a string with the specified character#Strnset (sstr1,ch,n)Sstr='1345'CH='R'N= 3sStr= n * ch + sstr1[3:]   PrintsStr Scan String#strpbrk (SSTR1,SSTR)Sstr='Cekjgdklab'sStr='Gka'NPos=-1 forCinchSSTR1:ifCinchSstr:npos=Sstr1.index (c) Break   PrintNPos flipping a string#Strrev (SSTR1)Sstr='ABCDEFG'sStr= Sstr1[::-1]   PrintsStr Find String#strstr (SSTR1,SSTR)Sstr='ABCDEFG'sStr='CDE'   PrintSstr1.find (SSTR) Split string#strtok (SSTR1,SSTR)Sstr='Ab,cde,fgh,ijk'sStr=','sStr= Sstr1[sstr1.find (SSTR) + 1:]   PrintSStr or S='Ab,cde,fgh,ijk'   Print(S.split (',') ) connection string delimiter=','MyList= ['Brazil','Russia','India',' China']   PrintDelimiter.join (MyList) Implementation of Addslashes in PHPdefAddslashes (s): D= {'"':'\\"',"'":"\ \ '"," /":"\\\0","\\":"\\\\"}       return "'. Join (D.get (c, c) forCinchs) s="John ' Johny ' Doe (a.k.a. \ "Super joe\") \\\0"   PrintsPrintaddslashes (s) displays only letters and numbersdefOnlycharnum (s,oth="'): S=S.lower (); Fomart='abcdefghijklmnopqrstuvwxyz013456789'        forCinchS:if  notCinchfomart:s= S.replace (c,"'); returns; Print(Onlystr ("a000 aa-b"))

Source: http://www.centoscn.com/python/2013/0807/1155.html

Python Common string manipulation

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.