Python 常用字符串操作
2018-01-11 10:58
344 查看
Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符串 #strcpy(sStr1,sStr) sStr= 'strcpy' sStr = sStr sStr= 'strcpy' print sStr 连接字符串 #strcat(sStr1,sStr) sStr= 'strcat' sStr = 'append' sStr+= sStr print sStr 查找字符 #strchr(sStr1,sStr) sStr= 'strchr' sStr = 's' nPos = sStr1.index(sStr) print nPos 比较字符串 #strcmp(sStr1,sStr) sStr= 'strchr' sStr = 'strch' print cmp(sStr1,sStr) 扫描字符串是否包含指定的字符 #strspn(sStr1,sStr) sStr= '1345678' sStr = '456' #sStrand chars both in sStrand sStr print len(sStrand sStr) 字符串长度 #strlen(sStr1) sStr= 'strlen' print len(sStr1) 将字符串中的大小写转换 #strlwr(sStr1) sStr= 'JCstrlwr' sStr= sStr1.upper() #sStr= sStr1.lower() print sStr 追加指定长度的字符串 #strncat(sStr1,sStr,n) sStr= '1345' sStr = 'abcdef' n = 3 sStr+= sStr[0:n] print sStr 字符串指定长度比较 #strncmp(sStr1,sStr,n) sStr= '1345' sStr = '13bc' n = 3 print cmp(sStr1[0:n],sStr[0:n]) 复制指定长度的字符 #strncpy(sStr1,sStr,n) sStr= '' sStr = '1345' n = 3 sStr= sStr[0:n] print sStr 将字符串前n个字符替换为指定的字符 #strnset(sStr1,ch,n) sStr= '1345' ch = 'r' n = 3 sStr= n * ch + sStr1[3:] print sStr 扫描字符串 #strpbrk(sStr1,sStr) sStr= 'cekjgdklab' sStr = 'gka' nPos = -1 for c in sStr1: if c in sStr: nPos = sStr1.index(c) break print nPos 翻转字符串 #strrev(sStr1) sStr= 'abcdefg' sStr= sStr1[::-1] print sStr 查找字符串 #strstr(sStr1,sStr) sStr= 'abcdefg' sStr = 'cde' print sStr1.find(sStr) 分割字符串 #strtok(sStr1,sStr) sStr= 'ab,cde,fgh,ijk' sStr = ',' sStr= sStr1[sStr1.find(sStr) + 1:] print sStr 或者 s = 'ab,cde,fgh,ijk' print(s.split(',')) 连接字符串 delimiter = ',' mylist = ['Brazil', 'Russia', 'India', 'China'] print delimiter.join(mylist) PHP 中 addslashes 的实现 def addslashes(s): d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"} return ''.join(d.get(c, c) for c in s) s = "John 'Johny' Doe (a.k.a. \"Super Joe\")\\\0" print s print addslashes(s) 只显示字母与数字 def OnlyCharNum(s,oth=''): s = s.lower(); fomart = 'abcdefghijklmnopqrstuvwxyz013456789' for c in s: if not c in fomart: s = s.replace(c,''); return s; print(OnlyStr("a000 aa-b"))
来源:http://www.centoscn.com/python/2013/0807/1155.html
相关文章推荐
- Python 常用字符串操作
- python 常用字符串操作
- python3 常用模块(时间、os路径及文件操作、字符串比较,文件内容比较等模块)
- python常用数据类型操作-字符串
- Python一些常用的字符串操作
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python中字符串的常用操作
- python字符串,列表常用操作
- Python字符串常用操作
- python 字符串常用操作汇总
- Python中常用操作字符串的函数与方法总结
- Python:str字符串常用操作
- python实操(5):python数据类型及常用操作,数字、字符串、元组、列表、字典、集合
- 【学神-RHEL7】P4-Python列表操作和字符串的常用方法
- Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】[原创]_python_脚本之家
- Python列表操作和字符串的常用方法
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python 常用字符串操作
- Python中字符串的常用操作总结