Python | Permutation of a given string using inbuilt function

Created with Sketch.

Python | Permutation of a given string using inbuilt function

A permutation, also called an “arrangement number” or “order”, is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.

Examples:

Input :  str = 'ABC'
Output : ABC 
         ACB 
         BAC 
         BCA 
         CAB 
         CBA
# Function to find permutations of a given string
from itertools import permutations
 
def allPermutations(str):
      
     # Get all permutations of string 'ABC'
     permList = permutations(str)
 
     # print all permutations
     for perm in list(permList):
         print (''.join(perm))
       
# Driver program
if __name__ == "__main__":
    str = 'ABC'
    allPermutations(str)

Output:

ABC
ACB
BAC
BCA
CAB
CBA

Leave a Reply

Your email address will not be published. Required fields are marked *