string manipulation in Python

Source: Internet
Author: User

#python string Manipulation" "1. Copying Strings" "#strcpy (SSTR1,SSTR2)SSTR1 ='strcpy'SSTR2=SSTR1SSTR1='Strcpy2'PrintSSTR2" "2. Connection string" "#strcat (SSTR1,SSTR2)SSTR1 ='strcat'SSTR2='Append'SSTR1+=SSTR2PrintSSTR1" "3. Find characters" "#STRCHR (SSTR1,SSTR2)SSTR1 ='STRCHR'SSTR2='R'NPos=Sstr1.index (SSTR2)PrintNPos" "4. Comparing Strings" "#strcmp (SSTR1,SSTR2)SSTR1 ='STRCHR'SSTR2='Strch'PrintCMP (SSTR1,SSTR2)" "5. Whether the scanned string contains the specified characters" "#strspn (SSTR1,SSTR2)SSTR1 ='12345678'SSTR2='456'#SStr1 and chars both in SSTR1 and SSTR2PrintLen (sStr1 andsStr2)" "6. String length" "#strlen (SSTR1)SSTR1 ='strlen'PrintLen (SSTR1)" "7. Convert lowercase characters in a string to uppercase characters" "#strlwr (SSTR1)SSTR1 ='jcstrlwr'SSTR1=Sstr1.upper ()PrintSSTR1" "8. Append a string of the specified length" "#strncat (sstr1,sstr2,n)SSTR1 ='12345'SSTR2='abcdef'N= 3SSTR1+=Sstr2[0:n]PrintSSTR1" "9. String specifying length comparison" "#strncmp (sstr1,sstr2,n)SSTR1 ='12345'SSTR2='123BC'N= 3PrintCMP (Sstr1[0:n],sstr2[0:n])" "10. Copy characters of the specified length" "#strncpy (sstr1,sstr2,n)SSTR1 ="'SSTR2='12345'N= 3SSTR1=Sstr2[0:n]PrintSSTR1" "11. String comparisons, case insensitive" "#stricmp (SSTR1,SSTR2)SSTR1 ='ABCEFG'SSTR2='ABCEFG'PrintCMP (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= 3SSTR1= n * ch + sstr1[3:]PrintSSTR1" "13. Scan Strings" "#strpbrk (SSTR1,SSTR2)SSTR1 ='Cekjgdklab'SSTR2='Gka'NPos=-1 forCinchSSTR1:ifCinchSstr2:npos=Sstr1.index (c) BreakPrintNPos" "14. Flipping a string" "#Strrev (SSTR1)SSTR1 ='ABCDEFG'SSTR1= Sstr1[::-1]PrintSSTR1" "15. Finding Strings" "#strstr (SSTR1,SSTR2)SSTR1 ='ABCDEFG'SSTR2='CDE'PrintSstr1.find (SSTR2)" "16. Splitting a string" "#strtok (SSTR1,SSTR2)SSTR1 ='Ab,cde,fgh,ijk'SSTR2=','SSTR1= Sstr1[sstr1.find (SSTR2) + 1:]PrintSStr1

Use the while and for output string values:

>>> Stru ='abc'print(len (stru))while (i<len (Stru)):    print(stru[i])    i+=1    ABC

 for inch Range (0,len (stri)):     Print (Stri[i])    ABCD

string manipulation in Python

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.