Python comes with a lot of built-in functions. One of the important functions is strip()
.
For strings in Python, the strip()
method is used to return the copy of the string in which all the characters have been stripped from the beginning and the ending of the string.
str_name.strip([characters])
If the character’s argument is not provided, all leading and trailing white spaces are removed from the string.
When a string’s character on the left or right doesn’t match with the rest of characters in the argument, it stops removing the leading characters.
s=" hihi ice hihi "print(s.strip())#All the white space in left and right are removedprint(s.strip(" hie"))#All the whitespace,h,i,e in the left and right string are removedprint(s.strip("rom"))#No characters were removed