首页
技术小册
AIGC
面试刷题
技术文章
MAGENTO
云计算
视频课程
源码下载
PDF书籍
「涨薪秘籍」
登录
注册
1、Slicing
2、strip()
3、lstrip()
4、rstrip()
5、removeprefix()
6、removesuffix()
7、replace()
8、re.sub()
9、split()
10、rsplit()
11、join()
12、upper()
13、lower()
14、capitalize()
15、islower()
16、isupper()
17、isalpha()
18、isnumeric()
19、isalnum()
20、count()
21、find()
22、rfind()
23、startswith()
24、endswith()
25、partition()
26、center()
27、ljust()
28、rjust()
29、f-Strings
30、swapcase()
当前位置:
首页>>
技术小册>>
Python合辑2-字符串常用方法
小册名称:Python合辑2-字符串常用方法
2、strip() strip()方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。 ``` s = ' hello '.strip() print(s) # hello s = '###hello###'.strip() print(s) # ###hello### ``` 在使用strip()方法时,默认去除空格或换行符,所以#号并没有去除。 可以给strip()方法添加指定字符,如下所示。 ``` s = '###hello###'.strip('#') print(s) # hello ``` 此外当指定内容不在头尾处时,并不会被去除。 ``` s = ' \n \t hello\n'.strip('\n') print(s) # # hello s = '\n \t hello\n'.strip('\n') print(s) # hello ``` 第一个\n前有个空格,所以只会去取尾部的换行符。 最后strip()方法的参数是剥离其值的所有组合,这个可以看下面这个案例。 ``` s = 'www.baidu.com'.strip('cmow.') print(s) # baidu ``` 最外层的首字符和尾字符参数值将从字符串中剥离。字符从前端移除,直到到达一个不包含在字符集中的字符串字符为止。 在尾部也会发生类似的动作。
上一篇:
1、Slicing
下一篇:
3、lstrip()
该分类下的相关小册推荐:
Python合辑5-格式化字符串
Python编程轻松进阶(二)
Python合辑7-集合、列表与元组
Python甚础Django与爬虫
Python合辑4-130个字符串操作示例
Python机器学习基础教程(上)
Python与办公-玩转Word
Python与办公-玩转PPT
Python合辑11-闭包函数
剑指Python(万变不离其宗)
Python合辑12-面向对象
Python合辑10-函数