Python string manipulation

Source: Internet
Author: User

#Python字符串操作

"' 1. Copy string ' '
#strcpy (SSTR1,SSTR2)
SSTR1 = ' strcpy '
SSTR2 = SStr1
SSTR1 = ' Strcpy2 '
Print SSTR2

"' 2. Connection string ' '
#strcat (SSTR1,SSTR2)
SSTR1 = ' strcat '
SSTR2 = ' Append '
SSTR1 + = SStr2
Print SSTR1

"3. Find the character '"
#strchr (SSTR1,SSTR2)
SSTR1 = ' STRCHR '
SSTR2 = ' R '
NPos = Sstr1.index (SSTR2)
Print NPos

"4. Compare String '"
#strcmp (SSTR1,SSTR2)
SSTR1 = ' STRCHR '
SSTR2 = ' Strch '
Print CMP (SSTR1,SSTR2)

5. Whether the scan 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)

"' 6. String length '
#strlen (SSTR1)
SSTR1 = ' strlen '
Print Len (SSTR1)

"7. Convert lowercase characters in a string to uppercase characters"
#strlwr (SSTR1)
SSTR1 = ' jcstrlwr '
SSTR1 = Sstr1.upper ()
Print SSTR1

"8. Append the string" "of the specified length
#strncat (Sstr1,sstr2,n)
SSTR1 = ' 12345 '
SSTR2 = ' abcdef '
n = 3
SSTR1 + = Sstr2[0:n]
Print SSTR1

"' 9. String specifying length comparison '
#strncmp (Sstr1,sstr2,n)
SSTR1 = ' 12345 '
SSTR2 = ' 123BC '
n = 3
Print CMP (Sstr1[0:n],sstr2[0:n])

"10. Copy the characters of the specified length"
#strncpy (Sstr1,sstr2,n)
SSTR1 = ' '
SSTR2 = ' 12345 '
n = 3
SSTR1 = Sstr2[0:n]
Print SSTR1

"11. String comparison, case insensitive"
#stricmp (SSTR1,SSTR2)
SSTR1 = ' ABCEFG '
SSTR2 = ' ABCEFG '
Print CMP (Sstr1.upper (), Sstr2.upper ())

"12. Replace the first n characters of a string with the specified character '
#strnset (Sstr1,ch,n)
SSTR1 = ' 12345 '
ch = ' R '
n = 3
SSTR1 = n * ch + sstr1[3:]
Print SSTR1

"' 13. 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

"' 14. Flip the string ' '
#strrev (SSTR1)
SSTR1 = ' ABCDEFG '
SSTR1 = Sstr1[::-1]
Print SSTR1

"15. Find the String '"
#strstr (SSTR1,SSTR2)
SSTR1 = ' ABCDEFG '
SSTR2 = ' CDE '
Print Sstr1.find (SSTR2)

"' 16. Split String ' '
#strtok (SSTR1,SSTR2)
SSTR1 = ' Ab,cde,fgh,ijk '
SSTR2 = ', '
SSTR1 = Sstr1[sstr1.find (SSTR2) + 1:]
Print SSTR1

Python string manipulation

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.