Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转
一、去空格及特殊符号 s.strip().lstrip().rstrip(',') 二、复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print(sStr2) 三、连接字符串 #strcat(sStr1,sStr2) sStr1 = 'strcat' sStr2 = 'append' sStr1 += sStr2 print(sStr1) 四、查找字符 #strchr(sStr1,sStr2) # < 0 为未找到 sStr1 = 'strchr' sStr2 = 's' nPos = sStr1.index(sStr2) print(nPos) 五、比较字符串 #strcmp(sStr1,sStr2) sStr1 = 'strchr' sStr2 = 'strch' print(cmp(sStr1,sStr2)) 六、扫描字符串是否包含指定的字符 #strspn(sStr1,sStr2) sStr1 = '12345678' sStr2 = '456' #sStr1 and chars both in sStr1 and sStr2 print(len(sStr1 and sStr2)) 七、字符串长度 #strlen(sStr1) sStr1 = 'strlen'