Python String strip()
method is used to remove spaces at the beginning and at the end of the string. The intermediate spaces are not removed. Just the trailing spaces from the begging of the string and from the end of the string are removed. If characters are provided to the strip() method provided characters are removed from the string.
strip() Method Syntax
The string strip() method has the following syntax. The string can be a string value or a string variable.
STRING.strip(CHARACTERS)
- STRING is a string value or string variable where the strip() method is executed.
- CHARACTERS is optional. If not specified the leading and trailing spaces are removed from the STRING. If single or more characters specified the specified chracter group is removed from the STRING.
Remove Leading and Trailing Spaces From String
The leading and trailing spaces can be removed from a string value or string variable. In other words, the spaces at the beginning and at the end of the string can be removed by using the strip() method. We do not provide any parameter to the strip() method.
a = " pythontect.com ".strip()
print(a)
b = " pythontect.com "
c = b.strip()
print(c)
pythontect.com pythontect.com
Remove Specified Characters Group From String
The strip() method can be also used to remove a specified single or more character which can be also called a character group. The specified character group is removed from the specified string value or string variable. The specified character group can be located at the start or end. We can call this trailing or leading characters are removed if they match with the specified character group.
a = "pythontect.com".strip(".com")
print(a)
b = "pythontect.com"
c = b.strip(".com")
print(c)
pythontect pythontect