Strip in Python

What Is Strip() In Python?

The Strip() method in Python is one of the built-in functions that come from the Python library.  The Strip() method in Python removes or truncates the given characters from the beginning and the end of the original string. The default behavior of the strip() method is to remove the whitespace from the beginning and at the end of the string. If you provide a string that has the white spaces at the beginning and end of the string, the strip() method will return the only string without these whitespaces at the beginning and the end.

 Syntax:

 string.strip([characters])

Example:

StripInPython_1 

Output:

StripInPython_2 

Explanation:

  • In the above example, before the strip method, it prints the actual string.
  • Strip method without a parameter removes the whitespaces from the beginning and end of the string.

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

Parameters That Strip() In Python Supports

Python Strip() method accepts character as an optional parameter. If a user provides optional parameter characters, then those characters will be eliminated from the beginning and the end of the actual string. 

Syntax:

string.strip()  // strip method without optional parameter.

             string.strip([characters])  // strip method with optional parameter.

Note:  If the user does not provide the optional parameter characters, then the strip() method will just cut the white spaces from the beginning and the end of the actual string as a default behavior.

Return Types of Strip() Method in Python:

The return type of the strip() method in Python is a string that only depends on the parameter.

  • If character parameters are not provided, any whitespaces at the front and end of the string will be truncated, and the original string will be returned without white space at the front and the end.
  • If character parameters are not provided, and if there are no white spaces at the front and end of the string, then the original string will be returned as it is.
  • If a character parameter is provided, and if the matching character is available at the front and end of the string, then they will be removed, and the rest of the string will be returned.
  • If a character parameter is provided, and if the matching character is not available at the front and end of the string, then the string will be returned as it is.

Example 1:

strip() without characters parameter

StripInPython_3 

Output:

StripInPython_4 

Explanation:

In the above example, you used string with white spaces and string without white spaces. 

The strip method is used without parameters. Hence, the first string’s white spaces are removed and returned. Then, in the second string, it is returned, as there are no whitespaces.

Example 2:

strip() with characters parameter

StripInPython_5

Output:

StripInPython_6 

Explanation:

  • In the above example, you used a string with a special character and a digit.
  • The strip method is used with parameter characters.
  • As the first-string matching parameter available, the strip removes the specified character and returns the string.
  • As the second-string matching parameter is available, the strip removes the specified character and returns the string.
  • As the third-string matching parameter was not available, the strip did not remove anything and returns the string as it is.

Strip() For Invalid Data Type:

The strip() method in python does support only the ‘String’ data type. This means that the strip() will work only on the string and not any other data type. So what happens if you tried with other data types?

Example:

StripInPython_7 

Output:

StripInPython_8 

Explanation:

  • In the above example, you did not use string for the strip method.
  • The strip method throws an error as the use of unsupported data type.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

Why Is the Python Strip() Function Used?

Strip() method in Python is very useful when the developer wants to remove the characters or whitespaces from the beginning or end of the string.  Let us see that in detail. 

  1. a)  The strip() helps to remove the characters from the beginning or end of the string for the characters that are passed as parameters to the strip().
  2. b)  It also helps to remove the whitespaces from the beginning or end of the string.
  3. c)  If there are no whitespaces in the string, and characters parameter not specified then the string will be returned as it is.
  4. d)  If there are whitespaces in the string, and no character parameters are specified, then the string will be returned after truncating the whitespaces in the string.
Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

Conclusion:

The string strip() method in python is built-in from Python.  It helps the developer to remove the whitespaces or specific characters from the string at the beginning and end of the string.

Strip() method in string accepts only one parameter which is optional and has characters. 

If the specified character is available at the beginning and end of the string, it will be removed, and the rest of the string will be returned.

If the specified character is not available in the string, then the original string will be returned as it is.

In this article, you have learned what the Python String Strip is, the Strip() method in Python, strip method character parameter, strip method with parameter, strip method invalid data type, why strip method is being used, and required working examples for you to play around.  

To learn more about it, please join the Python Training Course from Simplilearn that will teach you the fundamentals of Python, conditional statements, data operations, shell scripting, and Django. This certification course features 38 hours of applied learning and and 8 hour worth of top-quality online self-paced learning content. It is designed to offer you a true real-world and practical programming experience - which will go a long way in helping you build a rewarding career as a Python programming expert. 

Do you have any queries for us on this “strip with Python” article? Do feel free to leave your inputs, questions and feedback in the comments section of this page Our seasoned experts will review and respond to them very soon.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.