{"id":267,"date":"2018-07-12T23:22:31","date_gmt":"2018-07-12T15:22:31","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=267"},"modified":"2018-09-05T11:16:52","modified_gmt":"2018-09-05T03:16:52","slug":"how-to-permutate-a-string-in-python-3","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-permutate-a-string-in-python-3\/","title":{"rendered":"How to permutate a string in Python 3"},"content":{"rendered":"<p>To permutate a string is to change the order or arrangement of the characters that the string is made up of. Given that n is the number of characters, there are n! different ways to permutate a given string. <\/p>\n<p>This post shows how we can permutate a string in Python 3.<\/p>\n<h2>Without importing any Python 3 libraries<\/h2>\n<h3>Printing different permutation of string with duplicates<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef permutate_string(string, prefix = ''):\r\n    if len(string) == 0:\r\n        print(prefix)\r\n    else:\r\n        for i in range(len(string)):\r\n            rem = string&#x5B;0:i] + string&#x5B;i+1:]\r\n            permutate_string(rem, prefix + string&#x5B;i])\r\n\r\npermutate_string('abb')\r\n\r\n''' Output:\r\nabb\r\nabb\r\nbab\r\nbba\r\nbab\r\nbba\r\n'''\r\n<\/pre>\n<h3>Collecting different permutation of string with duplicates in a list<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef build_permutation_list(string, prefix='', permutation_list=&#x5B;]):\r\n    if len(string) == 0:\r\n        permutation_list.append(prefix)\r\n    else:\r\n        for i in range(len(string)):\r\n            rem = string&#x5B;0:i] + string&#x5B;i + 1:]\r\n            build_permutation_list(rem, prefix + string&#x5B;i], permutation_list)\r\n\r\npermutation_list = &#x5B;]\r\nbuild_permutation_list('abb', permutation_list=permutation_list)\r\n# permutation_list will contain the list of string variations from this point on\r\n<\/pre>\n<h3>Collecting different permutation of string without duplicates in a list<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\npermutation_list = &#x5B;]\r\nbuild_permutation_list('abb', permutation_list=permutation_list)\r\npermutation_list = set(permutation_list)\r\n<\/pre>\n<h2>With itertools.permutations library function<\/h2>\n<h3>Printing different permutation of string with duplicates<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom itertools import permutations\r\n\r\nstring_permutations = permutations('abb')\r\nfor string in string_permutations:\r\n    print(''.join(string))\r\n    \r\n''' Output:\r\nabb\r\nabb\r\nbab\r\nbba\r\nbab\r\nbba\r\n'''\r\n<\/pre>\n<h3>Printing different permutation of string without duplicates<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom itertools import permutations\r\n\r\n# Use the set function to remove duplicates\r\nstring_permutations = set(permutations('abb'))\r\nfor string in string_permutations:\r\n    print(''.join(string))\r\n\r\n''' Output:\r\nbba\r\nbab\r\nabb\r\n'''\r\n<\/pre>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-4j\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-4j&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-4j&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F267&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>To permutate a string is to change the order or arrangement of the characters that the string is made up of. Given that n is the number of characters, there are n! different ways to permutate a given string. <\/p>\n<p>This post shows how we can permutate a string in Python 3.<\/p>\n","protected":false},"author":1,"featured_media":1244,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[542,226,233,541,543],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-4j","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/267"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=267"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/267\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1244"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}