首页 › 参考手册 › 字符串方法 › str.strip()

Python str.strip() 方法

描述

去除字符串首尾的空白(或指定字符)。lstrip 只去左边,rstrip 只去右边。

语法

str.strip([chars])

参数

参数说明
chars要去除的字符集合,可选

返回值

新字符串

实例

print(repr("  hello  ".strip()))
print("--title--".strip("-"))
print(repr("line\n".rstrip("\n")))
以上实例输出结果为:
'hello'
title
'line'