{"id":75457,"date":"2023-11-06T10:04:27","date_gmt":"2023-11-06T04:34:27","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/"},"modified":"2025-01-06T19:31:08","modified_gmt":"2025-01-06T14:01:08","slug":"strip-in-python","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/","title":{"rendered":"Strip() Function in Python with Examples"},"content":{"rendered":"\n<p>If you've been coding in Python for a hot minute or just started dipping your toes into the vast ocean of its functionality, you've likely come across a situation where you needed your strings as clean and neat as a new pin. Enter the <code>strip()<\/code> function, Python's built-in whitespace bouncer that ensures your strings only keep what truly matters.<\/p>\n\n\n\n<p>This little powerhouse is part of Python's string methods - a toolkit that's as essential to a developer as a good coffee machine is to a morning routine. Now, imagine you've got a string. It's a good string, but it's surrounded by spaces or maybe some odd newline characters that are just crashing the party. That's not what you invited!<\/p>\n\n\n\n<p>The <code>strip()<\/code> method in Python gracefully handles these uninvited guests. It effortlessly trims the leading and trailing whitespace, including tabs (<code>\\t<\/code>), newlines (<code>\\n<\/code>), and the regular old spaces. And here's the kicker - it's not just limited to whitespace. <code>strip()<\/code> is customizable, meaning you can specify other characters to be stripped away as well.<\/p>\n\n\n\n<p>We're going to take this neat function for a spin with examples that'll show you how to clean up strings and make them presentable, whether it's prepping for a data analysis, parsing text files, or just making sure your outputs look as tidy as a pin. So, grab your metaphorical brooms, and let's start sweeping those strings clean!<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" class=\"courses-cta-title-link\">Python Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>11.5 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>51 Coding Exercises<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" class=\"courses-cta-button\">\n                Start Free Trial\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-strip\"><strong>What is Python strip()?<\/strong><\/h2>\n\n\n\n<p>Python strip() is a built-in function included in the <a href=\"https:\/\/www.mygreatlearning.com\/blog\/open-source-python-libraries\/\">Python library<\/a>. The strip() function removes leading and trailing spaces to return a copy of the original string. By default, the strip() method helps to remove whitespaces from the start and the end of the string.&nbsp;<\/p>\n\n\n\n<p>Python's <code>strip()<\/code> method is a string operation that is used to remove specific characters from both the beginning and the end of a string. By default, if no arguments are provided, <code>strip()<\/code> will remove all whitespace characters from the start and end of the string. Whitespace characters include spaces, tabs (<code>\\t<\/code>), newlines (<code>\\n<\/code>), and carriage returns (<code>\\r<\/code>).<\/p>\n\n\n\n<p>However, <code>strip()<\/code> is quite versatile. You can also specify a string of characters to be removed, which makes it a handy method for trimming unwanted characters, not just whitespace.<\/p>\n\n\n\n<p>Here's the general syntax:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr.strip(&#x5B;chars])\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>str<\/code> is the string you want to strip characters from.<\/li>\n\n\n\n<li><code>chars<\/code> is an optional parameter where you can specify the set of characters to remove. If omitted, <code>strip()<\/code> defaults to removing whitespace.<\/li>\n<\/ul>\n\n\n\n<p>Let's see <code>strip()<\/code> in action with some examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Using strip() without specifying chars\n# It will remove whitespaces from the beginning and the end of the string.\nexample = \"   Hello, World!   \"\nstripped_example = example.strip()\nprint(stripped_example)  # Output: \"Hello, World!\"\n\n# Using strip() with specified chars\n# It will remove all 'a', 'e', 'i', 'o', 'u' from the beginning and the end of the string.\nexample_with_chars = \"aeiouHello, World!aeiou\"\nstripped_example_with_chars = example_with_chars.strip('aeiou')\nprint(stripped_example_with_chars)  # Output: \"Hello, World!\"\n<\/code><\/pre>\n\n\n\n<p>It's important to note that <code>strip()<\/code> only removes characters from the ends of the string. If you have characters in the middle that you want to remove, you'd need a different approach, like <code>replace()<\/code> or a regular expression.<\/p>\n\n\n\n<p>In essence, <code>strip()<\/code> helps keep your strings clean and formatted just the way you need, which can be particularly useful in data parsing where precision is key.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"syntax-of-string-strip\"><strong>Syntax of String strip()<\/strong><\/h3>\n\n\n\n<p>Here is the syntax:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring.strip(chars)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"strip-parameters\"><strong>strip() Parameters<\/strong><\/h3>\n\n\n\n<p>Here is the strip() parameter:&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstrip(&#x5B;chars])\n<\/pre><\/div>\n\n\n<p>The characters are a set of strings specifying the set of characters whose leading and trailing spaces have to be removed.&nbsp;<\/p>\n\n\n\n<p>In case the optional characters parameter is not given, all leading and trailing whitespaces are removed from the string.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>chars<\/code> (optional) - a string specifying the set of characters to be removed from the beginning and the end of the target string. If the <code>chars<\/code> argument is not provided, the default behavior is to remove whitespace characters (spaces, tabs, newlines, etc.).<\/li>\n<\/ul>\n\n\n\n<p>Here's a bit more detail about the <code>chars<\/code> parameter:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Type<\/strong>: The <code>chars<\/code> argument is expected to be a string where each character in the string is treated as an individual character to be stripped.<\/li>\n\n\n\n<li><strong>Behavior<\/strong>:\n<ul class=\"wp-block-list\">\n<li>If <code>chars<\/code> is not specified or <code>None<\/code>, the <code>strip()<\/code> method will remove whitespace characters from the start and end of the string.<\/li>\n\n\n\n<li>If <code>chars<\/code> is given, the characters in the string will be stripped from the beginning and end of the string until a character not in <code>chars<\/code> is found.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Non-continuous sequence<\/strong>: The sequence of characters in <code>chars<\/code> does not need to be contiguous in the string for them to be stripped. <code>strip()<\/code> will remove all occurrences of the <code>chars<\/code> characters, not considering the sequence or frequency.<\/li>\n\n\n\n<li><strong>Case sensitivity<\/strong>: The method is case sensitive. If you specify <code>chars<\/code> as \"abc\", it will not remove uppercase \"A\", \"B\", or \"C\".<\/li>\n\n\n\n<li><strong>Middle characters<\/strong>: <code>strip()<\/code> does not remove any characters from the middle of the string, even if they match the <code>chars<\/code> string.<\/li>\n<\/ol>\n\n\n\n<p>Here's how you might use the <code>chars<\/code> parameter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Stripping a combination of characters\nexample = \"***Hello, World!!!***\"\nprint(example.strip('*!'))  # Output: \"Hello, World\"\n\n# Stripping characters without specifying 'chars'\n# It will strip the default whitespace characters\nprint(\"   Hello, World!   \".strip())  # Output: \"Hello, World!\"\n\n# Stripping whitespace and specific characters\nprint(\"   xyzHello, World!xyz   \".strip(' xyz'))  # Output: \"Hello, World!\"\n<\/code><\/pre>\n\n\n\n<p>Understanding how to use <code>strip()<\/code> and its <code>chars<\/code> parameter effectively can help you clean up strings for further processing or analysis.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"strip-return-value\"><strong>strip() Return Value<\/strong><\/h3>\n\n\n\n<p>Here is the strip() Return Value:<\/p>\n\n\n\n<p>It returns a copy of the string with both leading and trailing characters removed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-1-strip-method-in-python\"><strong>Example 1: strip() Method in Python<\/strong><\/h2>\n\n\n\n<p>Let us see an example to remove the leading and trailing spaces from a given strip in Python using strip() method.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>str1 = \"Welcome to Great Learning!\"\nafter_strip = str1.strip()<\/code><\/pre>\n\n\n\n<p>Here\u2019s what the output looks like:<\/p>\n\n\n\n<p>Welcome to Great Learning<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-2-strip-on-invalid-data-type\"><strong>Example 2: strip() on Invalid Data Type<\/strong><\/h2>\n\n\n\n<p>The strip() function in Python works only with strings. It will return an error if used for data types such as lists, tuples, etc.<\/p>\n\n\n\n<p>Let us take an example where it is used for lists:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mylist = &#091;\"a\", \"b\", \"c\", \"d\"]\nprint(mylist.strip()) <\/code><\/pre>\n\n\n\n<p>Here\u2019s what the output looks like:<\/p>\n\n\n\n<p>Traceback (most recent call last):<\/p>\n\n\n\n<p>&nbsp;&nbsp;File \"teststrip.py\", line 2, in &lt;module&gt;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;print(mylist.strip())<\/p>\n\n\n\n<p>AttributeError: 'list' object has no attribute 'strip'<\/p>\n\n\n\n<p>The output shows an error.&nbsp;<\/p>\n\n\n\n<p>The <code>strip()<\/code> function in Python works only with strings. It will return an error if used for data types such as lists or tuples, which can be confusing for those new to programming. To better understand how different data types interact, consider enrolling in a <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-fundamentals-for-beginners\" type=\"link\" id=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-fundamentals-for-beginners\">Free Python Course<\/a> that covers the basics of Python objects and error handling. This will help you avoid common pitfalls when performing data manipulation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-3-strip-without-character-parameter\"><strong>Example 3: strip() Without character parameter<\/strong><\/h2>\n\n\n\n<p>When used without a char parameter, here is how the strip() function works in Python:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>str1 = \"Welcome to Great Learning!\"\nafter_strip = str1.strip()\nprint(after_strip)\n\nstr2 = \"Welcome to Great Learning!\"\nafter_strip1 = str2.strip()\nprint(after_strip1)<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<p>Here\u2019s what the output looks like:<\/p>\n\n\n\n<p><strong>Welcome to Great Learning!<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-4-strip-passing-character-parameters\"><strong>Example 4: strip() Passing character parameters<\/strong><\/h2>\n\n\n\n<p>Here\u2019s how to use the strip() function in this case:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>str1 = \"****Welcome to Great Learning!****\"\nafter_strip = str1.strip(\"*\")\nprint(after_strip)\n\nstr2 = \"Welcome to Great Learning!\"\nafter_strip1 = str2.strip(\"99!\")\nprint(after_strip1)\nstr3 = \"Welcome to Great Learning!\"\nafter_strip3 = str3.strip(\"to\")\nprint(after_strip3)<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p><strong>Welcome to Great Learning!<\/strong><\/p>\n\n\n\n<p><strong>Welcome to Great Learning<\/strong><\/p>\n\n\n\n<p><strong>Welcome to Great Learning!<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-python-strip-function-is-used\"><strong>Why Python strip() function is used?<\/strong><\/h2>\n\n\n\n<p>The Python <code>strip()<\/code> function is used for several reasons, all related to string manipulation and tidying up string data:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Removing Whitespace<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Preprocessing<\/strong>: Before processing text (like parsing a file or preparing data for analysis), it's common to remove extraneous whitespace from the beginning and end of strings to maintain data consistency and accuracy.<\/li>\n\n\n\n<li><strong>User Input Cleaning<\/strong>: When receiving input from users, it\u2019s often necessary to strip away accidental leading or trailing spaces that could affect data processing or comparison (such as when a user enters a username or password).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Formatting<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Consistent Appearance<\/strong>: When displaying data, stripping strings ensures a uniform and clean appearance, free of unexpected padding.<\/li>\n\n\n\n<li><strong>Data Alignment<\/strong>: In output that\u2019s tabular or requires precise alignment, removing unnecessary whitespace can be crucial for readability.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Data Validation<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Avoiding False Mismatches<\/strong>: Trailing whitespaces can cause two otherwise identical strings to appear different. Stripping ensures that comparisons are made on the content that matters.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Sanitization<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Security<\/strong>: It can be a part of input sanitization to prevent malicious data within whitespaces, though this would typically require more comprehensive measures.<\/li>\n\n\n\n<li><strong>Preventing Errors<\/strong>: In configurations or data exchange, extra whitespace might lead to errors or misinterpretation of the data.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>File and String Parsing<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Preprocessing<\/strong>: Before splitting strings or extracting substrings, it's often helpful to strip them to ensure the separators are recognized correctly.<\/li>\n\n\n\n<li><strong>Clean Data Extraction<\/strong>: When data is extracted from files or over the network, leading and trailing characters that are not relevant to the actual data can be removed.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Custom Character Removal<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Flexibility<\/strong>: Beyond whitespace, <code>strip()<\/code> can be used to remove specific unwanted characters from the ends of a string, which is helpful when cleaning up special formatting or delimiters.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>The <code>strip()<\/code> Function is a simple yet powerful tool that plays a significant role in text preprocessing and formatting in Python, making it a staple in the string manipulation toolkit.<\/p>\n\n\n\n<p>Cleaning and sanitizing user input is a critical task for any tech practitioner. To streamline these technical workflows, the <strong>Learn Python with Generative AI<\/strong> course teaches you how to leverage ChatGPT to analyze, evaluate, and debug code effectively. <\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Johns Hopkins University<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/online.lifelonglearning.jhu.edu\/self-paced-online-python-with-generative-ai\" class=\"courses-cta-title-link\">Learn Python with Generative AI<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Learn Python programming fundamentals and apply Generative AI to write, debug, and refine code, progressing from foundational concepts to applied programming.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>Hands-on Learning<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>Duration: 10 Hours<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/online.lifelonglearning.jhu.edu\/self-paced-online-python-with-generative-ai\" class=\"courses-cta-button\">\n                Apply Now\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<p>By integrating AI as an interactive assistant, you can move beyond simple string manipulation to master complex file management, read\/write operations, and automated data processing in a professional developer environment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"frequently-asked-questions\"><strong>Frequently Asked Questions<\/strong><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1657541529973\"><strong class=\"schema-faq-question\"><strong>What is Strip () in Python?<\/strong><\/strong> <p class=\"schema-faq-answer\">Strip() is a function in the Python library that helps to return the copy of the string after removing the leading and trailing characters, whitespaces, and symbols that have been passed to the strip() function. Simply put, the Python strip() function is responsible for the removal of characters from the left and the right side of the string. It is done so when a set of character parameters are specified as an argument to the strip() function. In case there is no argument, it will remove whitespaces by default.\u00a0<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1657541540003\"><strong class=\"schema-faq-question\"><strong>What is a strip in Python example?<\/strong><\/strong> <p class=\"schema-faq-answer\">Here is an example of the strip() function in Python -<br\/><br\/>strinput = \"\u00a0 $$$$$\u00a0 No. 1 Welcome to GREAT LEARNING!! No. 1 $$$ \"\u00a0\u00a0\u00a0\u00a0\u00a0<br\/># use strip() function to remove the set of characters\u00a0\u00a0<br\/>res = strinput.strip ( ' $No. 10 !' ) # store result into res variable\u00a0\u00a0<br\/>print ( \" Given string is: \", strinput)\u00a0\u00a0<br\/>print ( \" After removing the set of characters: \", res)\u00a0\u00a0\u00a0<br\/>\u00a0\u00a0<br\/>str3 = ' 1 11 111 111 1111 Learn Python Tutorial 1111 111 11 1 '\u00a0\u00a0<br\/>str4 = str3. strip ( '1' )\u00a0\u00a0<br\/>print (\" \\n\u00a0 Given string is \", str3)\u00a0\u00a0<br\/>print (\" Stripping 1 from both ends of the string using strip ('1') function \", str4)\u00a0\u00a0<br\/>\u00a0\u00a0<br\/># define new string\u00a0\u00a0<br\/>str5 = '++++++Python Tutorial****** $$$$$'\u00a0\u00a0<br\/>print (\"\\n Given string is = \", str5)\u00a0\u00a0<br\/># use strip function to remove the symbols from both ends\u00a0\u00a0<br\/>str6 = str5. strip ( ' $*+' )\u00a0\u00a0<br\/>print (\" Stripping the '+', '*' and '$' symbols on both sides of the string is = \", str6)\u00a0\u00a0<br\/><br\/><strong>Output<\/strong><br\/><br\/>Here\u2019s what the output looks like:<br\/><br\/>Given string is:\u00a0 \u00a0 $$$$$\u00a0 No. 1 Welcome to GREAT LEARNING!! No. 1 $$$\u00a0<br\/>After the strip() function removes the set of characters:\u00a0 Welcome to GREAT LEARNING<br\/><br\/>Given string is \u00a0 1 11 111 111 1111 Learn Python Tutorial 1111 111 11 1<br\/>After strip() function Strips 1 from both ends of the string using strip ('1') function \u00a0 1 11 111 111 1111 Learn Python Tutorial 1111 111 11 1<br\/><br\/>\u00a0Given string is =\u00a0 ++++++Python Tutorial****** $$$$$<br\/>\u00a0Stripping the '+', '*' and '$' symbols on both sides of the string is =\u00a0 Python Tutorial<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1657541571230\"><strong class=\"schema-faq-question\"><strong>How do you type a strip in Python?<\/strong><\/strong> <p class=\"schema-faq-answer\">To remove any characters, whitespaces, or symbols, here is how to type strip() in Python -<br\/><br\/>strip( 'characters' )\u00a0\u00a0<br\/><br\/>Strip (\u2018chars\u2019) is the parameter that can be optional. If the developer does not pass any argument to the strip() function, it simply removes the whitespaces from the beginning and the end of the string to return the original string.<br\/>If the developer specifies a set of characters as an argument to the strip() function, it removes those characters or symbols from the beginning and the end of the string to return the original string.\u00a0<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1657541631538\"><strong class=\"schema-faq-question\"><strong>What is split and strip Python?<\/strong><\/strong> <p class=\"schema-faq-answer\">Python split() function breaks up a string to return the list of all strings. The programmer can specify the separator to the split() function which helps in splitting the string accordingly. If the programmer does not specify the separator, then the split() function will remove the whitespaces from the beginning and the end of the string by default.\u00a0<br\/>This method is very useful in Python as it helps to break the string into substrings according to the instance of the separator is specified by the programmer. In the case where max split is specified, the returning value will contain the list containing the specified number of elements plus one.\u00a0<br\/><br\/>Here\u2019s the syntax of the split() function in Python:<br\/><br\/>string.split(separator, maxsplit)<br\/><br\/>A separator is optional here. If it is not provided, the string will split at the whitespaces.<br\/>Max split refers to the maximum number of splits. If this value if not provided, then there is no limit on the number of splits.\u00a0<br\/>The output will return a list of strings<br\/><br\/>Let us check an example of the split() function in Python.<br\/><br\/>text= 'Welcome to Great Learning'<br\/><br\/># splits at space<br\/>print(text.split())<br\/><br\/>grocery = 'Milk, Bread, Eggs'<br\/><br\/># splits at ','<br\/>print(grocery.split(', '))<br\/><br\/># Splits at ':'<br\/>print(grocery.split(':'))<br\/><br\/>Output<br\/><br\/>Here\u2019s what the output looks like:<br\/><br\/>[\u2018Welcome\u2019, 'to', 'Great\u2019, \u2018Learning']<br\/>['Milk', 'Bread', \u2018Eggs\u2019]<br\/>['Milk, Bread, Eggs\u2019]<br\/><br\/>Python strip() function is primarily used to remove whitespaces from the start and the end of a string. For example,\u00a0<br\/><br\/>Str1=\" Hello, Learners\"\u00a0<br\/>print(str1.strip())\u00a0<br\/><br\/>Output : Hello, Learners<br\/><br\/>In the above example, we used string with parameter characters. In the output, the strip() method returns the string by removing the specified character at the beginning and the end of that string.\u00a0<\/p> <\/div> <\/div>\n\n\n\n<p>Embarking on a journey towards a career in data science opens up a world of limitless possibilities. Whether you\u2019re an aspiring data scientist or someone intrigued by the power of data, understanding the key factors that contribute to success in this field is crucial. The below path will guide you to become a proficient data scientist.<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table class=\"has-cyan-bluish-gray-background-color has-background\"><tbody><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/data-science\/courses\/certificates\" target=\"_blank\" rel=\"noreferrer noopener\">Data Science Course Certificates<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/data-science\/courses\/placements\" target=\"_blank\" rel=\"noreferrer noopener\">Data Science Course Placements<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/data-science\/courses\/syllabus\" target=\"_blank\" rel=\"noreferrer noopener\">Data Science Course Syllabus<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/data-science\/courses\/eligibility\" target=\"_blank\" rel=\"noreferrer noopener\">Data Science Course Eligibility<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>If you've been coding in Python for a hot minute or just started dipping your toes into the vast ocean of its functionality, you've likely come across a situation where you needed your strings as clean and neat as a new pin. Enter the strip() function, Python's built-in whitespace bouncer that ensures your strings only [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":75467,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36796],"content_type":[36248],"class_list":["post-75457","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-python","content_type-career-guide"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Strip() Function in Python with Examples<\/title>\n<meta name=\"description\" content=\"Get an in-depth understanding of what is strip in Python and its syntax with the help of examples in this blog.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Strip() Function in Python with Examples\" \/>\n<meta property=\"og:description\" content=\"Get an in-depth understanding of what is strip in Python and its syntax with the help of examples in this blog.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-06T04:34:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-06T14:01:08+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"688\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Strip() Function in Python with Examples\",\"datePublished\":\"2023-11-06T04:34:27+00:00\",\"dateModified\":\"2025-01-06T14:01:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/\"},\"wordCount\":2179,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/code-944499_1280.jpg\",\"keywords\":[\"python\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/\",\"name\":\"Strip() Function in Python with Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/code-944499_1280.jpg\",\"datePublished\":\"2023-11-06T04:34:27+00:00\",\"dateModified\":\"2025-01-06T14:01:08+00:00\",\"description\":\"Get an in-depth understanding of what is strip in Python and its syntax with the help of examples in this blog.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#faq-question-1657541529973\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#faq-question-1657541540003\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#faq-question-1657541571230\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#faq-question-1657541631538\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/code-944499_1280.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/code-944499_1280.jpg\",\"width\":1280,\"height\":688,\"caption\":\"strip in python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Strip() Function in Python with Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#faq-question-1657541529973\",\"position\":1,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#faq-question-1657541529973\",\"name\":\"What is Strip () in Python?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Strip() is a function in the Python library that helps to return the copy of the string after removing the leading and trailing characters, whitespaces, and symbols that have been passed to the strip() function. Simply put, the Python strip() function is responsible for the removal of characters from the left and the right side of the string. It is done so when a set of character parameters are specified as an argument to the strip() function. In case there is no argument, it will remove whitespaces by default.\u00a0\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#faq-question-1657541540003\",\"position\":2,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#faq-question-1657541540003\",\"name\":\"What is a strip in Python example?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Here is an example of the strip() function in Python -<br\\\/><br\\\/>strinput = \\\"\u00a0 $$$$$\u00a0 No. 1 Welcome to GREAT LEARNING!! No. 1 $$$ \\\"\u00a0\u00a0\u00a0\u00a0\u00a0<br\\\/># use strip() function to remove the set of characters\u00a0\u00a0<br\\\/>res = strinput.strip ( ' $No. 10 !' ) # store result into res variable\u00a0\u00a0<br\\\/>print ( \\\" Given string is: \\\", strinput)\u00a0\u00a0<br\\\/>print ( \\\" After removing the set of characters: \\\", res)\u00a0\u00a0\u00a0<br\\\/>\u00a0\u00a0<br\\\/>str3 = ' 1 11 111 111 1111 Learn Python Tutorial 1111 111 11 1 '\u00a0\u00a0<br\\\/>str4 = str3. strip ( '1' )\u00a0\u00a0<br\\\/>print (\\\" \\\\n\u00a0 Given string is \\\", str3)\u00a0\u00a0<br\\\/>print (\\\" Stripping 1 from both ends of the string using strip ('1') function \\\", str4)\u00a0\u00a0<br\\\/>\u00a0\u00a0<br\\\/># define new string\u00a0\u00a0<br\\\/>str5 = '++++++Python Tutorial****** $$$$$'\u00a0\u00a0<br\\\/>print (\\\"\\\\n Given string is = \\\", str5)\u00a0\u00a0<br\\\/># use strip function to remove the symbols from both ends\u00a0\u00a0<br\\\/>str6 = str5. strip ( ' $*+' )\u00a0\u00a0<br\\\/>print (\\\" Stripping the '+', '*' and '$' symbols on both sides of the string is = \\\", str6)\u00a0\u00a0<br\\\/><br\\\/><strong>Output<\\\/strong><br\\\/><br\\\/>Here\u2019s what the output looks like:<br\\\/><br\\\/>Given string is:\u00a0 \u00a0 $$$$$\u00a0 No. 1 Welcome to GREAT LEARNING!! No. 1 $$$\u00a0<br\\\/>After the strip() function removes the set of characters:\u00a0 Welcome to GREAT LEARNING<br\\\/><br\\\/>Given string is \u00a0 1 11 111 111 1111 Learn Python Tutorial 1111 111 11 1<br\\\/>After strip() function Strips 1 from both ends of the string using strip ('1') function \u00a0 1 11 111 111 1111 Learn Python Tutorial 1111 111 11 1<br\\\/><br\\\/>\u00a0Given string is =\u00a0 ++++++Python Tutorial****** $$$$$<br\\\/>\u00a0Stripping the '+', '*' and '$' symbols on both sides of the string is =\u00a0 Python Tutorial\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#faq-question-1657541571230\",\"position\":3,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#faq-question-1657541571230\",\"name\":\"How do you type a strip in Python?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"To remove any characters, whitespaces, or symbols, here is how to type strip() in Python -<br\\\/><br\\\/>strip( 'characters' )\u00a0\u00a0<br\\\/><br\\\/>Strip (\u2018chars\u2019) is the parameter that can be optional. If the developer does not pass any argument to the strip() function, it simply removes the whitespaces from the beginning and the end of the string to return the original string.<br\\\/>If the developer specifies a set of characters as an argument to the strip() function, it removes those characters or symbols from the beginning and the end of the string to return the original string.\u00a0\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#faq-question-1657541631538\",\"position\":4,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/strip-in-python\\\/#faq-question-1657541631538\",\"name\":\"What is split and strip Python?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Python split() function breaks up a string to return the list of all strings. The programmer can specify the separator to the split() function which helps in splitting the string accordingly. If the programmer does not specify the separator, then the split() function will remove the whitespaces from the beginning and the end of the string by default.\u00a0<br\\\/>This method is very useful in Python as it helps to break the string into substrings according to the instance of the separator is specified by the programmer. In the case where max split is specified, the returning value will contain the list containing the specified number of elements plus one.\u00a0<br\\\/><br\\\/>Here\u2019s the syntax of the split() function in Python:<br\\\/><br\\\/>string.split(separator, maxsplit)<br\\\/><br\\\/>A separator is optional here. If it is not provided, the string will split at the whitespaces.<br\\\/>Max split refers to the maximum number of splits. If this value if not provided, then there is no limit on the number of splits.\u00a0<br\\\/>The output will return a list of strings<br\\\/><br\\\/>Let us check an example of the split() function in Python.<br\\\/><br\\\/>text= 'Welcome to Great Learning'<br\\\/><br\\\/># splits at space<br\\\/>print(text.split())<br\\\/><br\\\/>grocery = 'Milk, Bread, Eggs'<br\\\/><br\\\/># splits at ','<br\\\/>print(grocery.split(', '))<br\\\/><br\\\/># Splits at ':'<br\\\/>print(grocery.split(':'))<br\\\/><br\\\/>Output<br\\\/><br\\\/>Here\u2019s what the output looks like:<br\\\/><br\\\/>[\u2018Welcome\u2019, 'to', 'Great\u2019, \u2018Learning']<br\\\/>['Milk', 'Bread', \u2018Eggs\u2019]<br\\\/>['Milk, Bread, Eggs\u2019]<br\\\/><br\\\/>Python strip() function is primarily used to remove whitespaces from the start and the end of a string. For example,\u00a0<br\\\/><br\\\/>Str1=\\\" Hello, Learners\\\"\u00a0<br\\\/>print(str1.strip())\u00a0<br\\\/><br\\\/>Output : Hello, Learners<br\\\/><br\\\/>In the above example, we used string with parameter characters. In the output, the strip() method returns the string by removing the specified character at the beginning and the end of that string.\u00a0\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Strip() Function in Python with Examples","description":"Get an in-depth understanding of what is strip in Python and its syntax with the help of examples in this blog.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Strip() Function in Python with Examples","og_description":"Get an in-depth understanding of what is strip in Python and its syntax with the help of examples in this blog.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2023-11-06T04:34:27+00:00","article_modified_time":"2025-01-06T14:01:08+00:00","og_image":[{"width":1280,"height":688,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Strip() Function in Python with Examples","datePublished":"2023-11-06T04:34:27+00:00","dateModified":"2025-01-06T14:01:08+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/"},"wordCount":2179,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280.jpg","keywords":["python"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/","url":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/","name":"Strip() Function in Python with Examples","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280.jpg","datePublished":"2023-11-06T04:34:27+00:00","dateModified":"2025-01-06T14:01:08+00:00","description":"Get an in-depth understanding of what is strip in Python and its syntax with the help of examples in this blog.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#faq-question-1657541529973"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#faq-question-1657541540003"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#faq-question-1657541571230"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#faq-question-1657541631538"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280.jpg","width":1280,"height":688,"caption":"strip in python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Strip() Function in Python with Examples"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#faq-question-1657541529973","position":1,"url":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#faq-question-1657541529973","name":"What is Strip () in Python?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Strip() is a function in the Python library that helps to return the copy of the string after removing the leading and trailing characters, whitespaces, and symbols that have been passed to the strip() function. Simply put, the Python strip() function is responsible for the removal of characters from the left and the right side of the string. It is done so when a set of character parameters are specified as an argument to the strip() function. In case there is no argument, it will remove whitespaces by default.\u00a0","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#faq-question-1657541540003","position":2,"url":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#faq-question-1657541540003","name":"What is a strip in Python example?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Here is an example of the strip() function in Python -<br\/><br\/>strinput = \"\u00a0 $$$$$\u00a0 No. 1 Welcome to GREAT LEARNING!! No. 1 $$$ \"\u00a0\u00a0\u00a0\u00a0\u00a0<br\/># use strip() function to remove the set of characters\u00a0\u00a0<br\/>res = strinput.strip ( ' $No. 10 !' ) # store result into res variable\u00a0\u00a0<br\/>print ( \" Given string is: \", strinput)\u00a0\u00a0<br\/>print ( \" After removing the set of characters: \", res)\u00a0\u00a0\u00a0<br\/>\u00a0\u00a0<br\/>str3 = ' 1 11 111 111 1111 Learn Python Tutorial 1111 111 11 1 '\u00a0\u00a0<br\/>str4 = str3. strip ( '1' )\u00a0\u00a0<br\/>print (\" \\n\u00a0 Given string is \", str3)\u00a0\u00a0<br\/>print (\" Stripping 1 from both ends of the string using strip ('1') function \", str4)\u00a0\u00a0<br\/>\u00a0\u00a0<br\/># define new string\u00a0\u00a0<br\/>str5 = '++++++Python Tutorial****** $$$$$'\u00a0\u00a0<br\/>print (\"\\n Given string is = \", str5)\u00a0\u00a0<br\/># use strip function to remove the symbols from both ends\u00a0\u00a0<br\/>str6 = str5. strip ( ' $*+' )\u00a0\u00a0<br\/>print (\" Stripping the '+', '*' and '$' symbols on both sides of the string is = \", str6)\u00a0\u00a0<br\/><br\/><strong>Output<\/strong><br\/><br\/>Here\u2019s what the output looks like:<br\/><br\/>Given string is:\u00a0 \u00a0 $$$$$\u00a0 No. 1 Welcome to GREAT LEARNING!! No. 1 $$$\u00a0<br\/>After the strip() function removes the set of characters:\u00a0 Welcome to GREAT LEARNING<br\/><br\/>Given string is \u00a0 1 11 111 111 1111 Learn Python Tutorial 1111 111 11 1<br\/>After strip() function Strips 1 from both ends of the string using strip ('1') function \u00a0 1 11 111 111 1111 Learn Python Tutorial 1111 111 11 1<br\/><br\/>\u00a0Given string is =\u00a0 ++++++Python Tutorial****** $$$$$<br\/>\u00a0Stripping the '+', '*' and '$' symbols on both sides of the string is =\u00a0 Python Tutorial","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#faq-question-1657541571230","position":3,"url":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#faq-question-1657541571230","name":"How do you type a strip in Python?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"To remove any characters, whitespaces, or symbols, here is how to type strip() in Python -<br\/><br\/>strip( 'characters' )\u00a0\u00a0<br\/><br\/>Strip (\u2018chars\u2019) is the parameter that can be optional. If the developer does not pass any argument to the strip() function, it simply removes the whitespaces from the beginning and the end of the string to return the original string.<br\/>If the developer specifies a set of characters as an argument to the strip() function, it removes those characters or symbols from the beginning and the end of the string to return the original string.\u00a0","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#faq-question-1657541631538","position":4,"url":"https:\/\/www.mygreatlearning.com\/blog\/strip-in-python\/#faq-question-1657541631538","name":"What is split and strip Python?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Python split() function breaks up a string to return the list of all strings. The programmer can specify the separator to the split() function which helps in splitting the string accordingly. If the programmer does not specify the separator, then the split() function will remove the whitespaces from the beginning and the end of the string by default.\u00a0<br\/>This method is very useful in Python as it helps to break the string into substrings according to the instance of the separator is specified by the programmer. In the case where max split is specified, the returning value will contain the list containing the specified number of elements plus one.\u00a0<br\/><br\/>Here\u2019s the syntax of the split() function in Python:<br\/><br\/>string.split(separator, maxsplit)<br\/><br\/>A separator is optional here. If it is not provided, the string will split at the whitespaces.<br\/>Max split refers to the maximum number of splits. If this value if not provided, then there is no limit on the number of splits.\u00a0<br\/>The output will return a list of strings<br\/><br\/>Let us check an example of the split() function in Python.<br\/><br\/>text= 'Welcome to Great Learning'<br\/><br\/># splits at space<br\/>print(text.split())<br\/><br\/>grocery = 'Milk, Bread, Eggs'<br\/><br\/># splits at ','<br\/>print(grocery.split(', '))<br\/><br\/># Splits at ':'<br\/>print(grocery.split(':'))<br\/><br\/>Output<br\/><br\/>Here\u2019s what the output looks like:<br\/><br\/>[\u2018Welcome\u2019, 'to', 'Great\u2019, \u2018Learning']<br\/>['Milk', 'Bread', \u2018Eggs\u2019]<br\/>['Milk, Bread, Eggs\u2019]<br\/><br\/>Python strip() function is primarily used to remove whitespaces from the start and the end of a string. For example,\u00a0<br\/><br\/>Str1=\" Hello, Learners\"\u00a0<br\/>print(str1.strip())\u00a0<br\/><br\/>Output : Hello, Learners<br\/><br\/>In the above example, we used string with parameter characters. In the output, the strip() method returns the string by removing the specified character at the beginning and the end of that string.\u00a0","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280.jpg",1280,688,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280-300x161.jpg",300,161,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280-768x413.jpg",768,413,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280-1024x550.jpg",1024,550,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280.jpg",1280,688,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280.jpg",1280,688,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280-640x688.jpg",640,688,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280-96x96.jpg",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/code-944499_1280-150x81.jpg",150,81,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"If you've been coding in Python for a hot minute or just started dipping your toes into the vast ocean of its functionality, you've likely come across a situation where you needed your strings as clean and neat as a new pin. Enter the strip() function, Python's built-in whitespace bouncer that ensures your strings only&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/75457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=75457"}],"version-history":[{"count":17,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/75457\/revisions"}],"predecessor-version":[{"id":116936,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/75457\/revisions\/116936"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/75467"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=75457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=75457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=75457"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=75457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}