Python string manipulation (string substitution, delete, intercept, copy, join, compare, find, include, Case transform, split, etc.)

Source: Internet
Author: User

Go to spaces and special symbols
S.strip (). Lstrip (). Rstrip (', ')
Copy string
#strcpy (sstr1,sstr2) sStr1 = ' strcpy ' sStr2 = sstr1sstr1 = ' strcpy2 ' Print sStr2
Connection string
#strcat (sstr1,sstr2) sStr1 = ' strcat ' sStr2 = ' append ' sStr1 + = Sstr2print sStr1
Find characters
#strchr (SSTR1,SSTR2) # < 0 for not found sStr1 = ' strchr ' sStr2 = ' s ' nPos = Sstr1.index (sStr2) Print NPos
Comparing strings
#strcmp (sstr1,sstr2) sStr1 = ' strchr ' sStr2 = ' strch ' Print cmp (SSTR1,SSTR2)
Scans whether a string contains the specified character
#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 case in a string
#strlwr (sStr1) sStr1 = ' jcstrlwr ' sStr1 = Sstr1.upper () #sStr1 = Sstr1.lower () print sStr1
Appends a string of the specified length
#strncat (sstr1,sstr2,n) sStr1 = ' 12345 ' sStr2 = ' abcdef ' n = 3sstr1 + = Sstr2[0:n]print sStr1
String specifying length Comparison
#strncmp (sstr1,sstr2,n) sStr1 = ' 12345 ' sStr2 = ' 123BC ' n = 3print cmp (sstr1[0:n],sstr2[0:n])
Copy characters of 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 character
#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
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# 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 ', ' \ \ ': ' \\\\ '}    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"))

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?

Python string manipulation (string substitution, delete, intercept, copy, join, compare, find, include, Case transform, split, etc.)

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.