首页 > 文章列表 > 如何使用python中strip函数?

如何使用python中strip函数?

python基础strip函数
329 2022-08-07

python中,str函数可以将某一类型的变量或者常量转换为字符串对象,而如果你想返回原字符串的副本,移除其中的前导和末尾字符,就要用到strip函数,本文将对python中strip函数做详细解释。

1、strip()

移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。

2、语法

str.strip([chars]);

3、参数

chars -- 移除字符串头尾指定的字符;

如果省略或为 None,则 chars 参数默认移除空格符;

实际上 chars 参数并非指定单个前缀或后缀,而是会移除参数值的所有组合。

4、返回值

返回移除字符串头尾指定的字符生成的新字符串。

5、使用实例

In [68]: " Strip fuction ".strip()
Out[68]: 'Strip fuction'
In [72]: "\tStrip fuction\n".strip()
Out[72]: 'Strip fuction'
In [73]: "\tStrip \n fuction\n".strip()
Out[73]: 'Strip \n fuction'