str.strip([chars])
返回原字符串的副本,移除其中的前导和末尾字符。
chars 参数为指定要移除字符的字符串。 如果省略或为 None,则 chars 参数默认移除空格符。实际上 chars 参数并非指定单个前缀或后缀;而是会移除参数值的所有组合。
举例:
print(' spacious '.strip()) #输出:spacious
print('www.example.com'.strip('cmowz.')) #输出:example
最外侧的前导和末尾 chars 参数值将从字符串中移除。 开头端的字符的移除将在遇到一个未包含于 chars 所指定字符集的字符时停止。
类似的操作也将在结尾端发生。 例如:
comment_string = '#....... Section 3.2.1 Issue #32 .......'
print(comment_string.strip('.#! ')) #输出:Section 3.2.1 Issue #32