python 4-6 如何去掉字符串中不需要的字符strip('-+*')/lstrip()/rstrip()/切片分段+/replace/sub/translate
来源:互联网 发布:青岛太阳软件 编辑:程序博客网 时间:2024/06/10 23:19
4-6 如何去掉字符串中不需要的字符
方法一,字符串strip() lstrip() rstrip() 去掉字符串两端字符
方法二,删除单个位置的字符,可以使用切片 + 拼接的方式
方法三,字符串的replace()方法或者正则表达式re.sub删除任意位置字符
方法四,字符串translate方法,可以同时删除多种不同的字符
方法一,字符串strip() lstrip() rstrip() 去掉字符串两端字符,默认参数是空格’ ‘,可以跟多个字符作为参数
>>> s = '***abc***def***'>>> s.strip('*') 去掉左右的*'abc***def'>>> s.lstrip('*') 去掉左边的*'abc***def***'>>> s.rstrip('*') 去掉右边的*'***abc***def'sa = '---*+****+++--abc--++*++def*+*++*----'sa.strip('-+*') 将左右两边的(-+*)全部删除'abc--++*++def'>>>
方法二,删除单个位置的字符,可以使用切片 + 拼接的方式
>>> s = "abc:def">>> s2 = s[:3] + s[4:]>>> s2'abcdef'
方法三,字符串的replace()方法或者正则表达式re.sub删除任意位置字符
通过replace需要调用多次才能最后替换完成s = "***+++\tabc***---\tdef**---\t**8">>> s'***+++\tabc***---\tdef**---\t**8'>>> s.replace("*",'').replace('-','').replace('+','').replace('\t','')'abcdef8'通过re.sub可以将需要替换的字符全部放在pattern中,由于是枚举多个字符,因此需要用[\*,\t,\-,\+] 或者 \* | \t | |- | \+>>> re.sub(r"[\*,\t,\-,\+]","",s)'abcdef8'>>> re.sub("\*|\\t|-|\+","",s)'abcdef8'>>>
方法四,字符串translate方法,可以同时删除多种不同的字符
>>> s = "zabc123xyza">>> s.translate(string.maketrans("abcxyz","xyzabc"))'cxyz123abcx'>>> s = "zabc123xyza">>> s.translate(None,"abc")'z123xyz'>>>
help(str.strip/lstrip/rstrip/str.translate/string.maketrans/re.sub/str.replace
>>> help(str.strip)Help on method_descriptor:strip(...) S.strip([chars]) -> string or unicode Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping(END) >>> help(str.lstrip)Help on method_descriptor:lstrip(...) S.lstrip([chars]) -> string or unicode Return a copy of the string S with leading whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping>>> >>> help(str.rstrip)Help on method_descriptor:rstrip(...) S.rstrip([chars]) -> string or unicode Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping>>> >>> help(str.translate)Help on method_descriptor:translate(...) S.translate(table [,deletechars]) -> string Return a copy of the string S, where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256 or None. If the table argument is None, no translation is applied and the operation simply removes the characters in deletechars.>>> help(string.maketrans)Help on built-in function maketrans in module strop:maketrans(...) maketrans(frm, to) -> string Return a translation table (a string of 256 bytes long) suitable for use in string.translate. The strings frm and to must be of the same length.>>> >>> help(re.sub) Help on function sub in module re:sub(pattern, repl, string, count=0, flags=0) Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used.>>> >>> >>> help(str.replace)Help on method_descriptor:replace(...) S.replace(old, new[, count]) -> string Return a copy of string S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.>>>
0 0
- python 4-6 如何去掉字符串中不需要的字符strip('-+*')/lstrip()/rstrip()/切片分段+/replace/sub/translate
- 【详解】python中字符串的strip(),lstrip(),rstrip()的含义
- python中strip\lstrip\rstrip的使用方法(字符串处理)
- Python 中 strip lstrip rstrip的使用方法
- strip,lstrip,rstrip Python
- python- strip lstrip rstrip
- Python-strip(),lstrip(),rstrip()
- python:strip()、rstrip()、lstrip()
- python-strip lstrip rstrip
- python中strip(),lstrip(),rstrip()函数的讲解
- Python中strip(),lstrip(), rstrip()使用方法
- python的strip(),lstrip()和rstrip()
- Python strip lstrip rstrip使用方法
- python strip lstrip rstrip 用法
- Python strip lstrip rstrip使用方法
- Python strip lstrip rstrip使用方法
- Python strip lstrip rstrip使用方法
- Python strip lstrip rstrip使用方法
- 算法训练 区间K大数
- RadAsm + OD 搭配编写和调试汇编程序
- 第七章 构造函数&this&静态&单例模式
- iOS程序中的内存分配 栈区堆区全局区
- scanf()输入数字和字符
- python 4-6 如何去掉字符串中不需要的字符strip('-+*')/lstrip()/rstrip()/切片分段+/replace/sub/translate
- intrusive_ptr源码分析
- 机器学习笔记之朴素贝叶斯分类算法
- Android SVG矢量资源的使用方法
- Spring+redis的整合
- 编程珠玑: 13章 搜索 13.1生成[0 ,maxval]范围内m各随机整数的有序序列 -------解题总结
- SpringMvc+Quartz执行多次的处理
- [Shell]一个特好用的查找命令 find + grep
- 深入理解Java内存模型