strip() function in Python
The 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 Python
Use the strip() function to remove characters or symbols from a given string
Let's consider an example to perform the strip() function by removing the leading or trailing characters from a given string in Python.
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 string
Let's consider an example to perform the strip() function by removing the leading or trailing whitespace from a given string in Python.
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() function
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:
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.
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week