Javatpoint Logo
Javatpoint Logo

strip() function in Python

The 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.

strip() function in Python

Syntax

Parameter:

  1. strip ('chars'): A strip() parameter is an optional parameter. So, if the programmer does not pass any parameter to strip() function, it removes the starting and ending whitespaces from the strings.
  2. If the set of a given parameter is passed to the strip() function, it removes the characters or symbols from the original string.

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 string

Let'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 string

Let'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() function

Prog.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:

  1. It helps to remove the characters from the leading and trailing of the original string based on the passed characters to the strip() function.
  2. If the user does not pass any character to the strip function, by default, it removes only the white space from both ends of the string.
  3. If there is no white space at the starting or end of the original string, it returns the original string without modifying it.
  4. If the passed character is not matched with the original string, the strip function returns the original string.

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA