August 17, 2024
Python removing extra spaces or characters using strip() function
Some times it happens you want to remove spaces or any other character from the right and left side from a string. So you can easily do that with using strip function. we called strip function on a string object and it requires one argument which is character which you want to remove. This parameter is optional if you do not pass parameter then it assume you want to remove spaces and if you want to remove any other character then spaces then you will have to pass the character parameter. strip function removes spaces or character from the left and right side of the string. The example of strip function is mentioned below:
Code
msgVariable = " Hello "
// below mentioned statement remove the spaces from the both side
modifiedMessage = msgVariable.strip()
print(modifiedMessage)
msgVariable = "Hello"
// below mentioned statement remove the o character from the string
modifiedMessage = msgVariable.strip("o")
print(modifiedMessage)