The strip() function in python removes the trailing and leading characters in the copy of the original string based on the arguments given. The method returns this copy as output.
string.strip([chars]) #where chars are those to remove from right & left
The strip() function takes a set of characters as its parameter. If chars are not provided the trailing and leading whitespaces are removed from the string.
Parameter | Description | Required / Optional |
---|---|---|
chars | characters to remove as trailing and leading characters | Optional |
The return value is always a string. Until the first match found, it searches from both left and right of the string and stops removing if it reaches the end of the string.
Input | Return Value |
---|---|
string | copy of string |
string1 = ",,,,,rrttgg.....python....rrr"
# Removing characters
string2 = string1.strip(",.grt")
print(string2)
Output:
python
string1 = " Hii Python! "
# Removes white spaces
after_strip = string1.strip()
Output:
Hii Python!