Python string manipulation (string substitution, delete, intercept, copy, join, compare, find, include, Case transform, split, etc.)

Source: Internet
Author: User

Go to spaces and special symbols
S.strip (). Lstrip (). Rstrip (',')
Copy string
# strcpy (SSTR1,SSTR2) ' strcpy '  ='strcpy2'print sStr2
Connection string
# strcat (SSTR1,SSTR2) ' strcat '  'append'+ = sStr2print sStr1
Find characters
# STRCHR (SSTR1,SSTR2) # < 0 for not found ' STRCHR '  's'= sstr1.index (sStr2)print NPos
Comparing strings
# strcmp (SSTR1,SSTR2) ' STRCHR '  'strch'print cmp (SSTR1,SSTR2)
Scans whether a string contains the specified character
# strspn (SSTR1,SSTR2) ' 12345678 '  '456'#sStr1 and chars both in SStr1 and sStr2print  and SSTR2)
String length
# strlen (SSTR1) ' strlen ' print len (SSTR1)
Converts the case in a string
# strlwr (SSTR1) ' jcstrlwr '  = sstr1.upper ()#sStr1 = Sstr1.lower ()print sStr1
Appends a string of the specified length
# strncat (sstr1,sstr2,n) ' 12345 '  'abcdef'= 3+ = sstr2[0:n]print sStr1
String specifying length Comparison
# strncmp (sstr1,sstr2,n) ' 12345 '  '123bc'= 3print cmp (Sstr1[0:n],sstr2[0:n])
Copy characters of specified length
# strncpy (sstr1,sstr2,n) "'  '12345'= 3= sstr2[0:n]print sStr1
Replaces the first n characters of a string with the specified character
# Strnset (sstr1,ch,n) ' 12345 '  'r'= 3= n * ch + sstr1[3:]print sStr1
Scan string
# strpbrk (SSTR1,SSTR2) ' Cekjgdklab '  'gka'     =-1 for in SSTR1: if   in sStr2:        = Sstr1.index (c)        breakprint NPos
Flip string
# Strrev (SSTR1) ' ABCDEFG '  = sstr1[::-1]print sStr1
Find string
# strstr (SSTR1,SSTR2) ' ABCDEFG '  'cde'print sstr1.find (SSTR2)
Split string
# strtok (SSTR1,SSTR2) ' Ab,cde,fgh,ijk '  ','= Sstr1[sstr1.find (SSTR2) + 1:]print  SSTR1# or 'ab,cde,fgh,ijk'print(S.split ( ' , '))
Connection string
' , '  = ['Brazil'Russia'India ' '  China ' ]print delimiter.join (mylist)
Implementation of Addslashes in PHP
defAddslashes (s): D= {'"':'\\"',"'":"\ \ '"," /":"\\\0","\\":"\\\\"}    return "'. Join (D.get (c, c) forCinchs) s="John ' Johny ' Doe (a.k.a. \ "Super joe\") \\\0"PrintsPrintAddslashes (s)
Show only letters and numbers
defOnlycharnum (s,oth="'): S2=S.lower (); Fomart='abcdefghijklmnopqrstuvwxyz0123456789'     forCinchS2:if  notCinchfomart:s= S.replace (c,"'); returns;Print(Onlystr ("a000 aa-b"))
Intercept string
str = ' 0123456789 -PrintStr[0:3]#Intercept first-to-third-digit charactersPrintstr[:]#truncate all characters of a stringPrintStr[6:]#Intercept the seventh character to the end of thePrintSTR[:-3]#intercept from the beginning to the third of the last characterPrintSTR[2]#interception of the third characterPrintSTR[-1]#Intercept the first character of the countdownPrintSTR[::-1]#create a string opposite to the original string orderPrintSTR[-3:-1]#intercept the characters before the third and last digitPrintStr[-3:]#intercept the third to the end of the countdownPrintSTR[:-5:-3]#What do you mean, you don't understand? 

Python string manipulation (string substitution, delete, intercept, copy, join, compare, find, include, Case transform, split, etc.)

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.