{"id":3713,"date":"2022-12-02T18:48:51","date_gmt":"2022-12-02T18:48:51","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3713"},"modified":"2022-12-02T18:48:52","modified_gmt":"2022-12-02T18:48:52","slug":"python-string-split","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-string-split\/","title":{"rendered":"Python String split() Method With Example"},"content":{"rendered":"<p style=\"text-align: center;\">Python String split<\/p>\n<p>Now, we are going to learn about the split method. This split method split\u2019s the string at the given separator, and then returns a list of strings. If we do not specify the separator, the string is split on the whitespaces. Let\u2019s have a look at a simple program, which tries to demonstrate the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3714 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-split-METHOD.jpg\" alt=\"\" width=\"628\" height=\"91\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-split-METHOD.jpg 628w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-split-METHOD-300x43.jpg 300w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/p>\n<h2 style=\"text-align: center;\">Python String split() Method<\/h2>\n<p>As you can see in the above program, we have a simple string, and then we are calling the split method. We also have specified the separator here, and the method is going to return the list of strings. Let\u2019s have a look at the output now.<\/p>\n<p>[&#8216;Sugar&#8217;, &#8216;Wheat&#8217;, &#8216;Rice&#8217;, &#8216;Butter&#8217;, &#8216;Cheese&#8217;]<\/p>\n<p>As you can see in the output, we have the string splitted at the given separator, and the method returned a list of the strings.<\/p>\n<p>Also, we can specify another argument, which is maxsplit, which simply specifies that at most maxsplit splits are done. Let\u2019s have a look at the program now, which tries to demonstrate the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3715 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-split-METHOD-1.jpg\" alt=\"\" width=\"621\" height=\"72\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-split-METHOD-1.jpg 621w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-split-METHOD-1-300x35.jpg 300w\" sizes=\"auto, (max-width: 621px) 100vw, 621px\" \/><\/p>\n<p>As you can see in the above program, we have a simple string, and then we are calling the split method. This time, we have specified the maxsplit argument as well. If you have a look at the output of the above program, we can find that the splits are done three times. Let\u2019s have a look at the output now.<\/p>\n<p>[&#8216;Sugar&#8217;, &#8216;Wheat&#8217;, &#8216;Rice&#8217;, &#8216;Butter, Cheese&#8217;]<\/p>\n<p>As you can see in the output, we have got the list of strings, and the splits were done three times as specified in the argument. So, as and when required, we can specify the maxsplit argument.<\/p>\n<p>Now, the thing is that if we do not specify the separator, it defaults to whitespace.<\/p>\n<p>Let\u2019s have a look at a simple program, which specifies the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3716 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-split-METHOD-2.jpg\" alt=\"\" width=\"633\" height=\"94\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-split-METHOD-2.jpg 633w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-split-METHOD-2-300x45.jpg 300w\" sizes=\"auto, (max-width: 633px) 100vw, 633px\" \/><\/p>\n<p>As you can see in the above program, we have a string, and then we are calling the split method. This time, we have not specified any separator, so it would be splitted on the whitespaces. Let\u2019s have a look at the output now, which would make a clear picture in your mind, about the output from the program.<\/p>\n<p>[&#8216;Hello&#8217;, &#8216;from&#8217;, &#8216;GyaniPandit&#8217;]<\/p>\n<p>As you can see, we have got a list of strings, which is splitted over the whitespaces.<\/p>\n<p>So, as and when required, we can make use of the split method, which split\u2019s our string at the given separator, and returns the list of strings. We can also provide an optional argument, which is the maxsplit. If we do not specify the separator, it defaults to whitespaces.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python String split Now, we are going to learn about the split method. This split method split\u2019s the string at the given separator, and then returns a list of strings. If we do not specify the separator, the string is split on the whitespaces. Let\u2019s have a look at a simple program, which tries to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3713","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3713"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3713\/revisions"}],"predecessor-version":[{"id":4198,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3713\/revisions\/4198"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}