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
原创粉丝点击