<整理> Python 字符串常用函数

来源:https://www.cnblogs.com/sshcy/p/8065113.html

侵权请联系本人!

1 string = "String!"
2 string.replace(old, new[, count])

函数功能:

       将字符串中的某一字串替换为新的字符串

参数含义:

       old:将被替换的子字符串

       new:用于替换old子字符串的新字符串

       count:最大替换次数

1 re.sub(pattern, repl, string, count=0, flags=0)

函数功能:

       模式化替换字符串中与正则表达式匹配的字串

参数含义:

        pattern:正则表达式形式的模式字符串

        repl:用于替换匹配成功的子字符串的新字符串

        string:待替换的目标字符串

        count:最大替换次数,默认值表示全部替换

        flags:具体用处不详

1 string.strip([chars])
2 string.lstrip([chars])
3 string.rstrip([chars])

函数功能:

        删除字符串两端(左端/右端)的连续字符串,被删除的字符串为某一字符集中可重复的任意数量字符的任意组合,被删除的字符串停止于第一个不属于指定字符集的字符

参数含义:

        [chars]:指定字符集,默认为"\n\t\r "

猜你喜欢

转载自www.cnblogs.com/icemaster/p/10344399.html