strip() 、lstrip() 、rstrip()

时间:2023-03-09 19:48:03
strip() 、lstrip() 、rstrip()

strip() 用于移除字符串开头和结尾的空格或换行符,如果指定参数,则表示移除指定的字符
lstrip() 用于移除字符串开头的空格或换行符,如果指定参数,则表示移除指定的字符
rstrip() 用于移除字符串结尾的空格或换行符,如果指定参数,则表示移除指定的字符

In [1]: str = '  hello world  '

In [2]: str.strip()
Out[2]: 'hello world' In [3]: str.lstrip()
Out[3]: 'hello world ' In [4]: str.rstrip()
Out[4]: ' hello world' In [6]: str.strip('d ')
Out[6]: 'hello worl'