Python 字符串操作
生活随笔
收集整理的這篇文章主要介紹了
Python 字符串操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.復制字符串
#strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print sStr22.連接字符串
#strcat(sStr1,sStr2) sStr1 = 'strcat' sStr2 = 'append' sStr1 += sStr2 print sStr13.查找字符
#strchr(sStr1,sStr2) sStr1 = 'strchr' sStr2 = 'r' nPos = sStr1.index(sStr2) print nPos4.比較字符串
#strcmp(sStr1,sStr2) sStr1 = 'strchr' sStr2 = 'strch' print cmp(sStr1,sStr2)5.掃描字符串是否包含指定的字符
#strspn(sStr1,sStr2) sStr1 = '12345678' sStr2 = '456' #sStr1 and chars both in sStr1 and sStr2 print len(sStr1 and sStr2)6.字符串長度
#strlen(sStr1) sStr1 = 'strlen' print len(sStr1)7.將字符串中的小寫字符轉換為大寫字符
#strlwr(sStr1) sStr1 = 'JCstrlwr' sStr1 = sStr1.upper() print sStr18.追加指定長度的字符串
#strncat(sStr1,sStr2,n) sStr1 = '12345' sStr2 = 'abcdef' n = 3 sStr1 += sStr2[0:n] print sStr19.字符串指定長度比較
#strncmp(sStr1,sStr2,n) sStr1 = '12345' sStr2 = '123bc' n = 3 print cmp(sStr1[0:n],sStr2[0:n])10.復制指定長度的字符
#strncpy(sStr1,sStr2,n) sStr1 = '' sStr2 = '12345' n = 3 sStr1 = sStr2[0:n] print sStr111.字符串比較,不區分大小寫
#stricmp(sStr1,sStr2) sStr1 = 'abcefg' sStr2 = 'ABCEFG' print cmp(sStr1.upper(),sStr2.upper())12.將字符串前n個字符替換為指定的字符
#strnset(sStr1,ch,n) sStr1 = '12345' ch = 'r' n = 3 sStr1 = n * ch + sStr1[3:] print sStr113.掃描字符串
#strpbrk(sStr1,sStr2) sStr1 = 'cekjgdklab' sStr2 = 'gka' nPos = -1 for c in sStr1:if c in sStr2:nPos = sStr1.index(c)break print nPos14.翻轉字符串
#strrev(sStr1) sStr1 = 'abcdefg' sStr1 = sStr1[::-1] print sStr115.查找字符串
python strstr
#strstr(sStr1,sStr2) sStr1 = 'abcdefg' sStr2 = 'cde' print sStr1.find(sStr2)16.分割字符串
#strtok(sStr1,sStr2) sStr1 = 'ab,cde,fgh,ijk' sStr2 = ',' sStr1 = sStr1[sStr1.find(sStr2) + 1:] print sStr1轉載于:https://www.cnblogs.com/GISerp/archive/2012/12/12/pyScript.html
總結
以上是生活随笔為你收集整理的Python 字符串操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: KM算法模板
- 下一篇: Windows系统回顾之Windows