strip()函数使用

str.strip([chars])

函数原型

声明:s为字符串,rm为要删除的字符序列

s.strip(rm)    删除s字符串中开头、结尾处,位于 rm删除序列的字符


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'

The outermost leading and trailing chars argument values are stripped from the string. Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in chars. A similar action takes place on the trailing end. For example:

>>> comment_string='#....... Section 3.2.1 Issue #32 .......'>>> comment_string.strip('.#! ')'Section 3.2.1 Issue #32'

strip的过程:
从头到尾扫描字符串,只要它在chars中就移除,直到遇到不在chars中的字符串停止,接着从尾到头扫描字符串,只要它在chars中就移除,直到遇到不在chars中的字符串停止

注意:
1. 当rm为空时,默认删除空白符(包括'\n', '\r',  '\t',  ' ')
例如:
代码如下:

>>> a = '     123'
>>> a.strip()
'123'
>>> a='\t\tabc'
'abc'
>>> a = 'sdff\r\n'
>>> a.strip()
'sdff'
2.这里的rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉。
例如 :
代码如下:

>>> a = '123abc'
>>> a.strip('21')
'3abc'   结果是一样的
>>> a.strip('12')
'3abc'


<think>嗯,用户想了解Pythonstrip函数使用方法和特点。根据提供的引用材料,我需要整理出清晰、全面的解释。 用户提供的引用材料挺详细的。引用[1]介绍了strip函数的基本概念和注意事项,引用[2]展示了链式调用可能导致意外结果的有趣例子,引用[3]说明了默认行为和指定字符去除的用法,引用[4]则给出了底层C代码实现片段。 应该重点说明strip的核心特点:默认去除空白字符、可指定任意字符集、处理字符串两端的特性。用户可能被引用[2]中的链式调用困惑,需要特别解释为什么多次调用会产生那种奇怪的结果——因为每次strip都是独立地从两端开始移除字符集合中的所有字符。 考虑到用户可能是Python初学者,解释时要避免过于深入底层实现,但可以简单提及strip的高效性(引用[4]的底层实现暗示了其性能良好)。同时要澄清一个常见误区:strip处理的不是子字符串而是字符集合。 示例代码需要覆盖基本用法、指定字符、以及展示引用[3]中的组合效果。特别要强调lstrip()和rstrip()的差异,这是用户容易混淆的点。最后用表格对比三个方法会更直观。 </think>以下是关于 Python 中 `strip()` 函数的详细说明,结合引用资料整理: --- ### 一、基本介绍 `strip()` 是 Python 字符串的内置方法,用于**移除字符串两端**的指定字符(默认移除空白符)。其变体包括: - `lstrip()`:仅移除**左侧**字符 - `rstrip()`:仅移除**右侧**字符 --- ### 二、使用方法 #### 1. 默认行为:移除空白符 ```python text = " Hello World! \n\t" print(text.strip()) # 输出: "Hello World!" # 移除两端的空格、换行符(\n)、制表符(\t) [^1][^3] ``` #### 2. 指定移除字符 ```python text = "xxxyPythonxxyyyx" print(text.strip(&#39;xy&#39;)) # 输出: "Python" # 从两端连续移除所有 &#39;x&#39; 或 &#39;y&#39; [^3][^4] ``` #### 3. 单侧移除 ```python text = "##Data###" print(text.lstrip(&#39;#&#39;)) # 输出: "Data###" (左端) print(text.rstrip(&#39;#&#39;)) # 输出: "##Data" (右端) [^3] ``` --- ### 三、核心特点 1. **字符集合处理** 参数 `chars` 是**字符集合**而非子字符串。例如 `strip("abc")` 会移除所有 `&#39;a&#39;`, `&#39;b&#39;`, `&#39;c&#39;` 的排列组合(如 `&#39;aa&#39;`, `&#39;bc&#39;` 等)[^3]。 2. **执行顺序** 从字符串两端向中间扫描,遇到**首个不在 `chars` 中的字符**即停止[^4]: ```python print("hello".strip(&#39;he&#39;)) # 输出: "llo" # 移除左侧连续的 &#39;h&#39; 和 &#39;e&#39;,右侧无匹配字符 ``` 3. **链式调用风险** 连续调用可能导致意外结果(引用[2]示例): ```python s = "hello" print(s.strip(&#39;h&#39;).strip(&#39;e&#39;)) # 输出: "llo" # 第一次 strip(&#39;h&#39;) -> "ello",第二次 strip(&#39;e&#39;) -> "llo" ``` --- ### 四、高级应用 #### 1. 清洗多字符组合 ```python data = "??$Price: 100$$" clean = data.strip(&#39;?$: &#39;) # 移除 ?、$、: 和空格 print(clean) # 输出: "Price: 100" ``` #### 2. 自定义移除逻辑 通过生成字符集实现复杂清洗: ```python import string text = "!!!NOISE@123abc" chars = string.punctuation # 所有标点符号 print(text.strip(chars)) # 输出: "NOISE@123" (右侧 &#39;abc&#39; 非标点保留) ``` --- ### 五、注意事项 | 要点 | 说明 | |---------------------|----------------------------------------------------------------------| | **返回值类型** | 始终返回新字符串,原字符串不变 [^1] | | **空白符定义** | 包括空格、`\n`, `\r`, `\t` [^3] | | **空字符串处理** | `""` 或全被移除字符字符串返回空字符串 [^1] | | **性能** | 时间复杂度 O(n),高效(底层 C 实现直接扫描边界)[^4] | --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值