strip() function in PythonThe strip() function is a predefined library function of Python. It is used to return the copy of the original string by removing the leading and trailing whitespaces, characters, and symbols passed to the strip() function. In other words, it is the Python string strip() function which removes characters from both the left and right end of the string by specifying a set of characters to the strip() function as an argument. By default, it removes whitespaces from starting and ending string if no argument is passed to the strip() function. SyntaxParameter:
Return value: It returns a copy of the original string by removing the set of characters or whitespaces from the original string. 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?Following are the reason to use the python strip function:
Conclusion:In the above topic, we have learned about the strip() function of the Python Library. It is a strip() function used to remove the characters or spaces from start and end of the original string. But, if the user does not pass any character to the strip() function, it only removes the whitespaces from leading and trailing of the main string. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/courses/263489.html