python 4-6 如何去掉字符串中不需要的字符strip('-+*')/lstrip()/rstrip()/切片分段+/replace/sub/translate
2017-01-22 18:36
507 查看
4-6 如何去掉字符串中不需要的字符
方法一,字符串strip() lstrip() rstrip() 去掉字符串两端字符
方法二,删除单个位置的字符,可以使用切片 + 拼接的方式
方法三,字符串的replace()方法或者正则表达式re.sub删除任意位置字符
方法四,字符串translate方法,可以同时删除多种不同的字符
方法一,字符串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 o b52f n 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. >>>
相关文章推荐
- python-16-如何去掉字符串中不需要的字符?
- JS如何去掉字符串里面不需要的字符
- 4-6如何去掉字符串中不需要的字符
- c#一个字符串中如何去掉相同的字符
- 如何去掉数字字符串里面的特殊字符
- 去掉字符串中不需要的字符
- python_如何去除字符串中不想要的字符?
- python_如何去除字符串中不想要的字符?
- c#一个字符串中如何去掉相同的字符
- 去掉字符串中不需要的字符
- C#中如何去掉字符串最后两位指定字符
- python如何去除字符串中不想要的字符
- 【整理】Python中,如何将反斜杠u类型(\uXXXX)的字符串,转换为对应的unicode的字符
- Android studio关于含有转义字符的字符串无法Log,以及如何去掉转义字符问题
- wpf 如何去掉字符串中的特殊字符,或者从字符串中去掉
- Python中,如何将反斜杠u类型(\uXXXX)的字符串,转换为对应的unicode的字符
- python去掉字符串中重复字符的方法
- 报表中如何去掉字符串中的首字符或末字符
- python中如何处理一个字符串中的每一个字符
- Python中 如何将一个字符串分成一个个字符;