语法:

str.strip([chars])

功能:移除str的首尾的指定字符串。

          处理对象:str

          指定字符串:chars,

          返回:处理后的字符串

          注意:只要头尾包含有指定字符序列中的字符就删除。参数字符串在该应用中是一个“字符集合”的概念。

举例:


#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
str1 = "00000003210Runoob01230000000"
print (str1.strip( '0' ))  # 去除首尾字符 0
##输出: 3210Runoob0123
 
str2 = "   Runoob      "   # 去除首尾空格
print (str2.strip())
##输出:Runoob

 


#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
str = "123abcrunoob321"
print (str.strip( '12' ))  # 字符序列为 12
##输出:3abcrunoob3

更多细节参考:www.runoob.com/python/att-string-strip.html

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐