美文网首页
strip()方法

strip()方法

作者: 天雨流芳hodo | 来源:发表于2019-08-22 14:11 被阅读0次

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。

注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。

不带参数

strip() 处理的时候,如果不带参数,默认是清除两边的空白符,如:\n, \r, \t, ' '

str = ' This is a sample!\n\r\t'
print(str.strip())

输出:

This is a sample!

带参数

strip() 带有参数的时候,这个参数可以理解一个要删除的字符的列表,是否会删除的前提是从字符串最开头和最结尾是不是包含要删除的字符,只要头尾有对应其中的某个字符即删除,不考虑顺序,直到遇到第一个不包含在其中的字符为止

str = '123This is a sample!32'
print(str.strip('123'))

输出:

This is a sample!

相关文章

  • strip()方法

    Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。 注意:该方法只能删除开头...

  • Python strip()方法

    描述 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。 语法 strip()方法语法...

  • python之函数常用小技巧

    1. 字符串处理方法:strip()函数 1.1 原型及使用方法 1.2 strip()函数的常用扩展 2. ra...

  • Python strip()、split()和rstrip()方

    1. Python strip() 语法描述: Python strip() 方法用于移除字符串头尾指定的字符(默...

  • Python strip()方法

    描述 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。 注意:该方法只能删...

  • Python strip()方法

    strip()方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列 注意:该方法只能删除开头或者是结尾...

  • 3-3如何去掉字符串中不需要的字符

    使用字符串strip()方法 strip()方法可以用来去掉字符串开始和结尾的空白,lstrip()只去掉左边的,...

  • 字符串的清除方法

    字符串的清除方法有:strip(),lstrip(),rstrip(), 1.strip():清除字符串两边 ...

  • Python_去除字符串中的空格

    作者:Gakki 01. strip() 方法 strip() :用于移除字符串头尾指定的字符(默认为空格)或字符...

  • python 字符串补充(strip、lstrip、rstrip

    strip、lstrip、rstrip使用方法: 字符串变量名.strip('指定字符')字符串变量名.lstri...

网友评论

      本文标题:strip()方法

      本文链接:https://www.haomeiwen.com/subject/eosnsctx.html