Python string operations (string replacement, deletion, interception, replication, connection, comparison, search, inclusion, Case sensitivity conversion, segmentation, etc.), pythonstring

Source: Internet
Author: User

Python string operations (string replacement, deletion, interception, replication, connection, comparison, search, inclusion, Case sensitivity conversion, segmentation, etc.), pythonstring

Remove spaces and special symbols

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

The Python strip () method is used to remove the specified characters (spaces by default) from the start and end of a string ).

Copy string

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

Connection string

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

Search for characters

# Strchr (sStr1, sStr2) #<0 indicates that sStr1 = 'strchr 'sStr2 = 's' nPos = sStr1.index (sStr2) print nPos is not found

Comparison string

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

Whether the scan string contains specified characters

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

String Length

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

Converts the uppercase and lowercase letters in a string.

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

Append a string of the specified length

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

String Length comparison

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

Copy characters of the specified length

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

Replaces the first n characters of a string with the specified characters.

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

Scan string

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

Flip string

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

Search for strings

#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 # Or s = 'AB, cde, fgh, ijk' print (s. split (','))

Connection string

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

Implementation of addslashes in PHP

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 sprint 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"))

Truncate string

Str = '000000' print str [0: 3] # print str [:] # capture all characters of a string from the first to the third. print str [:] # print str [: -3] # print str [2] # capture the third character print str [-1] # capture the first character print str [:: -1] # create a print str [-3:-1] string that is in the opposite order of the original string # capture the print str [-3:] character before the last and last digits # intercept the last third digit to the End of print str [:-5:-3] # reverse intercept. What do you mean?

This article introduces you to this topic. I hope you will be able to provide more support to the customer's home in the future.

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.