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

strip() function in Python

Syntax

Parameter:

  • strip, or "chars": An optional parameter is the strip() parameter. Therefore, the strip() function removes the beginning and ending whitespaces from the strings if the programmer does not pass any parameters.
  • In the event that the arrangement of a given boundary is passed to the strip() capability, it eliminates the characters or images from the first string.
  • Value of return: By removing the set of characters or whitespaces from the original string, it returns a copy of it.

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?

The reasons to use the Python strip function are as follows:

  • It assists with eliminating the characters from the main and following of the first string considering the past characters to the strip() capability.
  • The strip function removes only the white space from both ends of the string by default if the user does not pass any characters to it.
  • It returns the original string without making any changes if there is no white space at the beginning or end.
  • If the passed character isn't coordinated with the first string, the strip capability returns the first string.

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.






Latest Courses