python strip函数怎么使用

35次阅读
没有评论

共计 523 个字符,预计需要花费 2 分钟才能阅读完成。

strip 函数可以用来移除字符串的头尾指定字符,默认移除字符串两端的空格或换行符。
基本用法:

string.strip([chars])

其中,string表示要操作的字符串,chars表示要移除的字符集合。如果不指定 chars 参数,默认移除字符串两端的空格或换行符。
示例:

string = "hello world"
new_string = string.strip()
print(new_string)  # 输出:hello world

如果要移除字符串中指定的字符,可以通过传入 chars 参数来实现。
示例:

string = "hello world"
new_string = string.strip("hld")
print(new_string)  # 输出:ello wor

在上面的示例中,strip函数移除了字符串 "hello world" 中的字符 "h""l""d"
另外,还有 lstrip 函数和 rstrip 函数分别用于移除字符串左侧和右侧的指定字符。用法与 strip 函数类似。
更多用法可以参考 Python 官方文档:https://docs.python.org/3/library/stdtypes.html#str.strip</p>

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-22发表,共计523字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)