`strip()`: 去除字符串两边的空格。
`lstrip()`: 去除字符串左边的空格。
`rstrip()`: 去除字符串右边的空格。
这些函数默认会去除空格字符,但也可以去除其他指定的字符。例如,使用`replace()`函数可以去除字符串中间的空格。
下面是一些示例代码:
使用 strip() 函数去除字符串两边的空格
string = " * it is blank space test * "
print(string.strip()) 输出:* it is blank space test *
使用 lstrip() 函数去除字符串左边的空格
string = " * it is blank space test * "
print(string.lstrip()) 输出:* it is blank space test *
使用 rstrip() 函数去除字符串右边的空格
string = " * it is blank space test * "
print(string.rstrip()) 输出:* it is blank space test *
使用 replace() 函数去除字符串中间的空格
string = "python 字符串 中间 空格"
print(string.replace(" ", "")) 输出:python字符串中间空格
请根据您的具体需求选择合适的函数