s="s,fadsafdsafasf"
print(s.strip("s,"))
输出结果是预计的 fadsafdsafasf
但是!但是!
print(s.strip("s,f"))
输出结果居然是 adsafdsafa
![]() | 1 easylee 2018-06-04 10:08:27 +08:00 ![]() |
2 ThirdFlame 2018-06-04 10:09:39 +08:00 ![]() >>> print(s.strip("s,")) fadsafdsafasf >>> print(s.strip("s,f")) adsafdsafa >>> print(s.strip("fs,")) adsafdsafa >>> 显然是 1 个字符 1 个字符处理的 |
![]() | 3 aheadlead 2018-06-04 10:11:33 +08:00 ![]() RTFM |
![]() | 4 virusdefender 2018-06-04 10:11:39 +08:00 ![]() str.strip([chars]) Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped: >>> >>> ' spacious '.strip() 'spacious' >>> 'www.example.com'.strip('cmowz.') 'example' |
![]() | 5 loryyang 2018-06-04 10:11:54 +08:00 ![]() 去读下 API,这是你有坑,兄弟 不要经常以为你有了大发现,人人都在用的东西,哪有那么容易错,先自我检查,是不是自己蠢了 |
![]() | 6 aheadlead 2018-06-04 10:12:16 +08:00 ![]() |
7 polaa 2018-06-04 10:12:16 +08:00 ![]() 。。。 我觉得没问题,strip return 不就是这么 return 的么 |
8 eurokingbai2 2018-06-04 10:13:25 +08:00 ![]() 没明白楼主想要什么字符串,python 这输出不对么? |
![]() | 9 shuax 2018-06-04 10:21:51 +08:00 ![]() RTFM |
![]() | 10 liuxey 2018-06-04 10:24:41 +08:00 ![]() 语言内置的基础 API 要是有问题那相关的帖子和新闻早就爆出来了 我举个不恰当的例子:如果你访问不了百度,你觉得是百度挂了还是你网络连接有问题 |
![]() | 11 Troevil 2018-06-04 10:29:14 +08:00 ![]() strip 和 trim 还是有区别的 |
![]() | 12 dassh 2018-06-04 10:32:36 +08:00 ![]() >>> 'abcde'.strip('ba') 'cde' |
![]() | 13 kkhu2004 OP |
![]() | 14 kkhu2004 OP |
![]() | 17 aheadlead 2018-06-04 13:29:36 +08:00 @recall704 #16 $ python3 Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> s = '12345' >>> s = s[1:-1] >>> print(s) 234 >>> |
![]() | 18 kkhu2004 &nsp; OP @recall704 暂时不知道,目前刚刚学,只练习到了这地步。 楼上( 15 楼)的方法是替换所有的相关字符串,不适用。 |
![]() | 19 mulog 2018-06-05 05:25:31 +08:00 ![]() >>> s="s,fadsafdsafasf" >>> target = 's,f' >>> print(s[len(target) if s.startswith(target) else 0: len(s) - len(target) if s.endswith(target) else len(s)]) adsafdsafasf |
![]() | 20 thinker3 2018-06-05 09:40:37 +08:00 ![]() |
![]() | 21 Pending 2021-02-24 17:59:59 +08:00 ![]() 这个坑害我不浅,`'WKHS.US.TXT'.rstrip('.US.TXT')` 将返回 `WKH` 而不是预期的 `WKHS`。 |