- 描述:
strip()
方法用于移除字符串头尾指定的字符(默认为空格)或字符序列
Tip:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符
- 语法
str.strip([chars]) # 其中chars参数为 字符串头尾指定的要移除的字符序列
- 参数和返回值
- 参数:字符串头尾指定的要移除的字符序列
- 返回值:返回移除字符串头尾hid的字符序列生成的新字符串
- 🌰1
str = "*****this is **string** example....wow!!!*****"
print(str.strip('*')) # 移除字符串中头尾指定的字符'*'
# 输出:this is **string** example....wow!!!==>中间部分的*并没有移除
- 🌰2
str = "123abcrunoob321"
print(str.strip('12')) # 移除字符串中指定的字符'12',只要出现'1'或者'2'都移除,不管顺序
# 输出:3abcrunoob3