Various operations of strings in Python

Source: Internet
Author: User
Tags join strlen in python

S.strip (). Lstrip (). Rstrip (', ') go to spaces and special symbols
Copy string
Python

The code is as follows Copy Code

#strcpy (SSTR1,SSTR2)
SSTR1 = ' strcpy '
SSTR2 = SStr1

SSTR1 = ' Strcpy2 '

Print SSTR2

Connection string

The code is as follows Copy Code

Python

#strcat (SSTR1,SSTR2)

SSTR1 = ' strcat '

SSTR2 = ' Append '

SSTR1 + + SSTR2

Print SSTR1

Find characters

The code is as follows Copy Code

< 0 not Found

Python

#strchr (SSTR1,SSTR2)

SSTR1 = ' STRCHR '

SSTR2 = ' s '

NPOs = Sstr1.index (SSTR2)

Print NPOs

Comparing strings

The code is as follows Copy Code

Python

#strcmp (SSTR1,SSTR2)

SSTR1 = ' STRCHR '

SSTR2 = ' Strch '

Print CMP (SSTR1,SSTR2)

Whether the scanned string contains the specified characters

The code is as follows Copy Code

Python

#strspn (SSTR1,SSTR2)

SSTR1 = ' 12345678 '

SSTR2 = ' 456 '

#sStr1 and chars both in SSTR1 and SSTR2

Print Len (SSTR1 and SSTR2)

String length

The code is as follows Copy Code

Python

#strlen (SSTR1)

SSTR1 = ' strlen '

Print Len (SSTR1)

Converts capitalization in a string

The code is as follows Copy Code

Python

#strlwr (SSTR1)

SSTR1 = ' jcstrlwr '

SSTR1 = Sstr1.upper ()

#sStr1 = Sstr1.lower ()

Print SSTR1

Append a string of the specified length

The code is as follows Copy Code

Python
#strncat (Sstr1,sstr2,n)

SSTR1 = ' 12345 '

SSTR2 = ' abcdef '

n = 3

SSTR1 + + sstr2[0:n]

Print SSTR1

String specifying length Comparisons

The code is as follows Copy Code

Python

#strncmp (Sstr1,sstr2,n)

SSTR1 = ' 12345 '

SSTR2 = ' 123BC '

n = 3

Print CMP (Sstr1[0:n],sstr2[0:n])

Copy characters of a specified length

The code is as follows Copy Code

Python

#strncpy (Sstr1,sstr2,n)

SSTR1 = ' '

SSTR2 = ' 12345 '

n = 3

SSTR1 = Sstr2[0:n]

Print SSTR1

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

The code is as follows Copy Code

Python

#strnset (Sstr1,ch,n)

SSTR1 = ' 12345 '

ch = ' R '

n = 3

SSTR1 = n * ch + sstr1[3:]

Print SSTR1

Scan string

The code is as follows Copy Code

Python

#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 a string

The code is as follows Copy Code

Python

#strrev (SSTR1)
SSTR1 = ' ABCDEFG '
SSTR1 = Sstr1[::-1]

Print SSTR1

Find string

The code is as follows Copy Code

Python

#strstr (SSTR1,SSTR2)

SSTR1 = ' ABCDEFG '
SSTR2 = ' CDE '

Print Sstr1.find (SSTR2)

Split string

The code is as follows Copy Code

Python

#strtok (SSTR1,SSTR2)

SSTR1 = ' Ab,cde,fgh,ijk '

SSTR2 = ', '

SSTR1 = Sstr1[sstr1.find (SSTR2) + 1:]

Print SSTR1

Or

The code is as follows Copy Code

s = ' Ab,cde,fgh,ijk '

Print (S.split (', '))

Connection string

The code is as follows Copy Code

Python
delimiter = ', '
MyList = [' Brazil ', ' Russia ', ' India ', ' ', ']

Print Delimiter.join (mylist)

Implementation of Addslashes in PHP
Python
def addslashes (s):
D = {' ': ', ' ', ' ' ': ' \ ', ' ': ' \ ', ' \ ': ' \ '
Return ". Join (D.get (c, c) for C in s)

s = "John ' Johny ' Doe" (a.k.a. "Super Joe") \ "
Print S
Print Addslashes (s)

Show only letters and numbers

The code is as follows Copy Code

Python

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

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.