Please note, this is a STATIC archive of website www.simplilearn.com from 27 Mar 2023, cach3.com does not collect or store any user information, there is no "phishing" involved.

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.

Basics to Advanced - Learn It All!

Caltech PGP Full Stack DevelopmentExplore Program
Basics to Advanced - Learn It All!

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.

Learn From The Best Mentors in the Industry!

Automation Testing Masters ProgramExplore Program
Learn From The Best Mentors in the Industry!

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.

Become a Creator of Products that Delight Users

Free Webinar | 29 March, Wednesday | 8 PM ISTRegister Now
Become a Creator of Products that Delight Users

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.

Get the Coding Skills You Need to Succeed

Full Stack Development-MEANExplore Program
Get the Coding Skills You Need to Succeed

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.
Don't miss out on the opportunity to become a Certified Professional with Simplilearn's Post Graduate Program in Full Stack Web Development. Enroll Today!

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

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

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