python function Tutorial: Python string operations (string substitutions, deletions, intercepted, copied, connection, comparison, look, comprising a case conversion, division, etc.)

This article describes the Python string operations (string replace, delete, capture, copy, connect, compare, find, contain, case conversion, division, etc.), need a friend at the reference
to spaces and special symbols

s.strip().lstrip().rstrip(',')

Python strip () method used to remove the head and tail of the specified character string (default is a space).

Copy the string

#strcpy(sStr1,sStr2)
sStr1 = 'strcpy'
sStr2 = sStr1
sStr1 = 'strcpy2'
print sStr2

Connection String

#strcat(sStr1,sStr2)
sStr1 = 'strcat'
sStr2 = 'append'
sStr1 += sStr2
print sStr1

Find character

#strchr(sStr1,sStr2)
# < 0 为未找到
sStr1 = 'strchr'
sStr2 = 's'
nPos = sStr1.index(sStr2)
print nPos

String comparison

#strcmp(sStr1,sStr2)
sStr1 = 'strchr'
sStr2 = 'strch'
print cmp(sStr1,sStr2)

Whether to scan the string contains the specified character

#strspn(sStr1,sStr2)
sStr1 = '12345678'
sStr2 = '456'
#sStr1 and chars both in sStr1 and sStr2
print len(sStr1 and sStr2)

String length

#strlen(sStr1)
sStr1 = 'strlen'
print len(sStr1)

The string case conversion

#strlwr(sStr1)
sStr1 = 'JCstrlwr'
sStr1 = sStr1.upper()
#sStr1 = sStr1.lower()
print sStr1

Additional string of specified length

#strncat(sStr1,sStr2,n)
sStr1 = '12345'
sStr2 = 'abcdef'
n = 3
sStr1 += sStr2[0:n]
print sStr1

Comparative specified length

#strncmp(sStr1,sStr2,n)
sStr1 = '12345'
sStr2 = '123bc'
n = 3
print cmp(sStr1[0:n],sStr2[0:n])

Copy the characters specified length

#strncpy(sStr1,sStr2,n)
sStr1 = ''
sStr2 = '12345'
n = 3
sStr1 = sStr2[0:n]
print sStr1

The first n characters of the string is replaced with the specified character

#strnset(sStr1,ch,n)
sStr1 = '12345'
ch = 'r'
n = 3
sStr1 = n * ch + sStr1[3:]
print sStr1

Scan string

#strpbrk(sStr1,sStr2)
sStr1 = 'cekjgdklab'
sStr2 = 'gka'
nPos = -1
for c in sStr1:
  if c in sStr2:
    nPos = sStr1.index(c)
    break
print nPos

Flip string

#strrev(sStr1)
sStr1 = 'abcdefg'
sStr1 = sStr1[::-1]
print sStr1

Find string

#strstr(sStr1,sStr2)
sStr1 = 'abcdefg'
sStr2 = 'cde'
print sStr1.find(sStr2)

Split string

#strtok(sStr1,sStr2)
sStr1 = 'ab,cde,fgh,ijk'
sStr2 = ','
sStr1 = sStr1[sStr1.find(sStr2) + 1:]
print sStr1
#或者
s = 'ab,cde,fgh,ijk'
print(s.split(','))

Connection String

delimiter = ','
mylist = ['Brazil', 'Russia', 'India', 'China']
print delimiter.join(mylist)

Implementation in PHP addslashes

def addslashes(s):
  d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"}
  return ''.join(d.get(c, c) for c in s)
  
s = "John 'Johny' Doe (a.k.a. \"Super Joe\")\\\0"
print s
print addslashes(s)

Show only letters and numbers

def OnlyCharNum(s,oth=''):
  s2 = s.lower();
  fomart = 'abcdefghijklmnopqrstuvwxyz0123456789'
  for c in s2:
    if not c in fomart:
      s = s.replace(c,'');
  return s; 
print(OnlyStr("a000 aa-b"))

Interception 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] #逆序截取,具体啥意思没搞明白?

I write to you, for everyone to recommend a very wide python learning resource gathering, click to enter , there is a senior programmer before learning to share

Experience, study notes, there is a chance of business experience, and for everyone to carefully organize a python to combat zero-based item of information,

Python day to you on the latest technology, prospects, learning needs little comment on the details of
the article on the introduction to this

Released five original articles · won praise 0 · Views 907

Guess you like

Origin blog.csdn.net/haoxun10/article/details/104663341