strip() function in PythonThe strip() capability is a predefined library capability of Python. By removing the leading and trailing whitespaces, characters, and symbols passed to the strip() function, it is used to return a copy of the original string. To put it another way, a set of characters is passed as an argument to the Python string strip() function, which then removes characters from both the left and right ends of the string. If no argument is given to the strip() function, it removes whitespaces from the beginning and end of the string by default. strip() function in PythonSyntaxParameter:
Use the strip() function to remove characters or symbols from a given stringLet's consider an example to perform the strip() function by removing the leading or trailing characters from a given string in Python. Strip.py Output Given string is: $$$$$ No. 1 Welcome to JAVATPOINT!! No. 1 $$$ After removing the set of characters: Welcome to JAVATPOINT Given string is 1 11 111 111 1111 Learn Python Programming Tutorial 1111 111 11 1 Stripping 1 from both ends of the string using strip ('1') function 1 11 111 111 1111 Learn Python Programming Tutorial 1111 111 11 1 Given string is = ++++++Python Programming Tutorial****** $$$$$ Stripping the '+', '*' and '$' symbols on both sides of the string is = Python Programming Tutorial Use the strip() function to remove whitespaces from a given stringLet's consider an example to perform the strip() function by removing the leading or trailing whitespace from a given string in Python. Strip2.py Output Given string is: Welcome to the World! After removing whitespaces from an original string: Welcome to the World! Given string is: Learn Python programming After removing whitespaces from an original string: Learn Python programming In the above program, we use the strip() function to remove whitespaces from starting and ending of the given string, but it does not remove whitespace in between the strings. Program to take any string from user and remove any characters or symbols using strip() functionProg.py Output Enter the string *** $$ Welcome to JavaTpoint. Learn Python programming !!! && Your string is *** $$ Welcome to JavaTpoint. Learn Python programming !!! && Enter any character or symbol that you don't want to see in the string * $ Wel ! & After performing the strip() function, Your string is come to JavaTpoint. Learn Python programming Why is the Python strip() function used?The reasons to use the Python strip function are as follows:
Conclusion:We have learned about the Python Library's strip() function in the preceding topic. It is a strip() capability used to eliminate the characters or spaces from start and end of the first string. However, the strip() function only removes whitespace from the main string's leading and trailing spaces if the user does not pass any characters to it. Next TopicGradient Descent Algorithm |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India