We often encounter a lot of whitespace in the processing of the problem, a one to remove the manual is not what we programmers should do, today this tip of the article script home to tell you how to use Python to remove the space in the string.
Let's first create a string variable s with n spaces around it, see the code:
Copy the Code code as follows:
>>> s = "Home of the script"
>>>
Remove the string space, in Python has its built-in method, do not need us to build the wheel.
Lstrip: Delete the left space
This string method removes the space before the start position of the string s.
Copy the Code code as follows:
>>> S.lstrip ()
' Home of the script '
Rstrip: Delete right-connected spaces
This built-in method can delete all the spaces at the end of the string and see the following demo code:
Copy the Code code as follows:
>>> S.rstrip ()
' Home of the script '
Strip: Remove spaces at both ends
Sometimes we read the contents of the file, each line of 2 sides have a space, can be removed all at once, the word character has a built-in strip () method can do.
Copy the Code code as follows:
>>> s = "Home of the script"
>>> S.strip ()
' Home of the script '
Script House Tip: You can use the Dir (str) method to get a list of all methods of STR strings.