Python中的strip()函数的用法

函数:string.strip()

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

一、函数说明

strip()

语法:str.strip([rm]);

参数说明

rm: 移除字符串str开头、结尾处,位于rm序列的字符。

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

二、实例


1.参数rm为空时,默认删除空白符(包括' ', ' ',  ' ',  ' ')

a = "   123abc "

print(a.strip())

输出为:123abc

2.参数rm不为空时,删除序列是只要(开头或结尾)上的字符在删除序列内

比如:

a = "123abc"

print(a.strip("12"))

输出为:3abc

print(a.strip("21"))

输出也是:3abc

原文地址:https://www.cnblogs.com/hjhsysu/p/5712244.html