Original 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 (',')
Copy string
#'strcpy'sStr2 ='strcpy2'print sStr2
Connection string
#'strcat'append'sStr1 + = sStr2print sStr1
Find characters
#STRCHR (sstr1,sstr2)#'strchr's'nPos = sstr1.index (sStr2)Print NPos
Comparing strings
#'strchr'strch'print cmp (sstr1,sstr2)
Scans whether a string contains the specified character
#'12345678'456'# sStr1 andchars both in SStr1 and SSTR2 andSStr2)
String length
#'strlen'print len (sStr1)
Converts the case in a string
#'jcstrlwr'sStr1 = sstr1.upper ()#sStr1 = sstr1.lower ()print sStr1
Appends a string of the specified length
#'12345'abcdef'n = 3sStr1 + = sstr2[0:n]print sStr1
String specifying length Comparison
#'12345'123bc'n = 3print cmp (sstr1[0:n],sstr2[0:n])
Copy characters of specified length
# '12345'n = 3sStr1 = sstr2[0:n]print sStr1
Replaces the first n characters of a string with the specified character
#'12345'r'n = 3sStr1 = n * ch + sstr1[3:]print sStr1 /c9>
Scan string
#'cekjgdklab'gka'nPos = 1 in. sstr2:npos = breakPrint NPos
Flip string
#'abcdefg'sStr1 = sstr1[::-1]print sStr1
Find string
#'abcdefg'cde 'print sstr1.find (sStr2)
Split string
#'ab,cde,fgh,ijk','sStr1 = sstr1[sstr1.find (sStr2) + 1:]print sStr1 #'ab,cde,fgh,ijk'print (s.split (','))
Connection string
','mylist = ['Brazil'Russia'India'China']print Delimiter.join (mylist)
Implementation of Addslashes in PHP
DefAddslashes (s): d = {‘"‘:‘\\"‘,"‘":"\\‘","":" "\\\0", " \ \":"\\\\"} return " . Join (d.get (c, C) for c in s) s = "John ' Johny ' Doe (A.K.A. \" Super joe\ ") \\\0"print sprint add Slashes (s) Show only letters and numbers
def onlycharnum (s,oth= " S.lower (); Fomart = ' abcdefghijklmnopqrstuvwxyz0123456789 ' for c in s2: if not c in fomart:s = s.replace (c," ); return s; print (onlystr ( "a000 aa-b
Intercept string
str = ' 0123456789′
Print str[0:3] #截取第一位到第三位的字符
Print str[:] #截取字符串的全部字符
Print str[6:] #截取第七个字符到结尾
Print str[:-3] #截取从头开始到倒数第三个字符之前
Print str[2] #截取第三个字符
Print str[-1] #截取倒数第一个字符
Print str[::-1] #创造一个与原字符串顺序相反的字符串
Print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符
Print str[-3:] #截取倒数第三位到结尾
Print str[:-5:-3] #逆序截取, What do you mean, you don't understand?
Processing of strings in Python