- strip() 的含义:用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
注意:只能删除开头或是结尾的字符,不能删除中间部分的字符。 - 删除字符串的空格
>>>str1 = "123 123 123 "
>>>print(str1)
123
# 删除字符串两侧的空格
>>>print(str1.strip())
123
# 删除字符串右侧的空格
>>>print(str1.rstrip())
123
# 删除字符串左侧的空格
>>>print(str1.lstrip())
123
- 删除符号条件的字符串
>>>str1 = "hello jack"
>>>print(str1)
hello jack
# 删除 str1 字符串两侧符合条件的字符串
>>>print(str1.strip("hk"))
ello jac
>>>print(str1.rstrip("hk"))
hello jac
>>>print(str1.lstrip("hk"))
ello jack
本文详细介绍了Python中strip()函数的使用方法,包括如何利用该函数去除字符串两端的空格和特定字符,以及如何区分使用lstrip()和rstrip()函数进行左侧和右侧的字符去除。通过实例展示了strip()函数的强大功能。
1266





