Skip to main content
 首页 » 编程设计

python之Python 3 中的 strip 不是 "working"吗

2025年05月04日25yyy_WW

我有以下字符串:

s = 'sd sdasd sas sas zxxx df xx de ' 

当我使用 s.strip('x')我得到以下结果:

'sd sdasd sas sas zxxx df xx de ' 

为什么 strip() 没有删除所有 'x' 字符?

请您参考如下方法:

来自 Python 3.6 文档:

str.strip([chars]): Return a copy of the string with the leading and trailing characters removed.

这意味着 s.strip('x') 仅从字符串的开头和结尾删除 x(例如 "xxxabcxx"。 strip('x') == "abc") 如果您需要替换字符串中的字符,您可能需要使用 str.replace() 方法。