Python中的strip()函数的用法

函数:string.strip()

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

 

一、函数说明

strip()

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

 

参数说明

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

 

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

 

二、实例


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

a = "   123abc\n\t\r"

print(a.strip())

输出为:123abc

 

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

比如:

a = "123abc"

print(a.strip("12"))

输出为:3abc

 

print(a.strip("21"))

输出也是:3abc

 

posted @ 2016-07-27 18:56  hjhsysu  阅读(1131)  评论(0编辑  收藏  举报