博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python的string.strip(s[, chars])方法的各种小细节
阅读量:7083 次
发布时间:2019-06-28

本文共 1031 字,大约阅读时间需要 3 分钟。

hot3.png

下面的英文说明是官方给出:

string.strip(s[, chars])

Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on.

Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions.

下面例子中字符以tab抬头,以空格结尾。

line='    hello happybks! 'print '*'+line.strip()+'*'print '*'+line.strip(' ')+'*'print '*'+line.strip('    ')+'*'print '*'+line.strip('h')+'*'

输出结果如下:

*hello happybks!**hello happybks!**hello happybks!**    hello happybks! *

可以发现不传参数,则会把字符串开头和结尾的空格、tab全部删除,中间的空格和tab不会

传空格或者tab参数,子串传仍然会把字符串开头和结尾的无论空格还是tab都一并删除

当传入的参数是其他参数时,字符串开头结尾不是该参数字符串,则没有任何效果

但是如果字符串的开头和结尾是其他字符串,并且传入的参数也是这个字符串,那么会将字符串开头和结尾的参数串全部清掉,无论有多少个。但是区分大小写。

例如,下面的例子:

line2='haaaaahhaaaaaaahHhhh'print '*'+line2.strip('h')+'*'

结果输出:

*aaaaahhaaaaaaahH*

转载于:https://my.oschina.net/happyBKs/blog/379428

你可能感兴趣的文章
.net web端导出Excel个人的看法
查看>>
12.29.作业
查看>>
项目管理初探
查看>>
keras入门--Mnist手写体识别
查看>>
服务器开发中的多进程,多线程及多协程
查看>>
1139 观光公交
查看>>
poj3159
查看>>
[Python爬虫] 之二十一:Selenium +phantomjs 利用 pyquery抓取36氪网站数据
查看>>
java 把json对象中转成map键值对
查看>>
extjs grid行插入进度条
查看>>
Extjs 页面布局以及dataview的使用
查看>>
滴滴研发笔记题,亮灯问题
查看>>
装饰模式(Decorator)
查看>>
几何画板二次函数系的制作
查看>>
[设计模式]PHP设计模式之单例模式
查看>>
树的高度
查看>>
iOS.KVC.setValue:forKey:
查看>>
转:设计模式六大原则(3):依赖倒置原则
查看>>
转:快速排序的一个小问题没想明白,求助各位
查看>>
eclipse rcp 打包出适合不同操作系统和操作位数.
查看>>