strip() method in Python String can be used to remove any leading or trailing spaces from a given string by default. We can use this method to remove any given character in a Python String.
Syntax
string.strip(characters)
s = " codeofgeeks "
print(s.strip())
O/P
codeofgeeks (without space)
Instead of space, we can also remove any other character(s) from a given string in a leading or trailing positions. Like,
s = "endcodeofgeeksend"
print(s.strip('end'))
O/P
codeofgeeks
Addition to this, we also have lstrip() and rstrip() in Python.
lstrip() : It is used to remove given character(s) from left position of the string.
rstrip() : It is used to remove given character(s) from right position of the string.
s = " cog "
print(s.lstrip()) # prints 'cog '
print(s.rstrip()) # prints ' cog'
That’s all here.
