{"id":3699,"date":"2022-12-02T18:43:29","date_gmt":"2022-12-02T18:43:29","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3699"},"modified":"2022-12-02T18:43:31","modified_gmt":"2022-12-02T18:43:31","slug":"python-string-partition","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-string-partition\/","title":{"rendered":"Python String partition() Method With Example"},"content":{"rendered":"<p style=\"text-align: center;\">Python String partition<\/p>\n<p>Now, we are going to learn about the partition method. Using this method, the string is split at the first occurrence of the specified separator. This method returns a 3 \u2013 tuple, containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, a 3 \u2013 tuple is returned, which contains the string itself, followed by two empty strings.<\/p>\n<p>Let\u2019s have a look at a simple program, which demonstrates the partition method.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3700 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-partition-METHOD.jpg\" alt=\"\" width=\"634\" height=\"74\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-partition-METHOD.jpg 634w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-partition-METHOD-300x35.jpg 300w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><\/p>\n<h2 style=\"text-align: center;\">Python String partition() Method<\/h2>\n<p>As you can see in the above program, we have a string, and then we are trying to call the partition method. We have specified the string &#8216;from&#8217; as the separator. The partition method returns a 3 \u2013 tuple, which contains the part before the separator, the separator itself, and the part after the separator.<\/p>\n<p>Let\u2019s have a look at the output of the above program.<\/p>\n<p>(&#8216;Hello &#8216;, &#8216;from&#8217;, &#8216; GyaniPandit&#8217;)<\/p>\n<p>As you can see, we have got a 3 \u2013 tuple, which contains the part before the separator, then the separator itself,\u00a0 followed by the part after the separator. You can try the program for some other values as well.<\/p>\n<p>Let\u2019s say that we now give a separator, which is not there in the string, in such a situation, we are going to get a 3 \u2013 tuple, that contains the string, followed by two empty strings.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3701 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-partition-METHOD-1.jpg\" alt=\"\" width=\"632\" height=\"89\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-partition-METHOD-1.jpg 632w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-partition-METHOD-1-300x42.jpg 300w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><\/p>\n<p>As you can see in the above program, we again have a string, and there we are trying to call the partition method. The separator that we are providing here, is not there in the string. So, we are going to get a 3 \u2013 tuple, which contains the string itself, followed by two empty strings.<\/p>\n<p>So, as and when required, we can make use of the partition method in our python programs. Remember that here, we are going to give a separator, and the partition method is going to return a 3 \u2013 tuple, which contains the part before the separator, the separator itself, and the part after the separator. If the separator that we have specified is not found, then we get a 3 \u2013 tuple that contains the string itself, followed by two empty strings.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python String partition Now, we are going to learn about the partition method. Using this method, the string is split at the first occurrence of the specified separator. This method returns a 3 \u2013 tuple, containing the part before the separator, the separator itself, and the part after the separator. If the separator is not [&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-3699","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\/3699","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=3699"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3699\/revisions"}],"predecessor-version":[{"id":4195,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3699\/revisions\/4195"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}