Computer >> Computer tutorials >  >> Programming >> Python

Python program for the permutation of a given string inbuilt function in python


String is given. Our task is to display permutation of given string. Here solve this problem in python using inbuilt function permutations (iterable).

Example

Input : string = 'XYZ'
Output : XYZ
XZY
YXZ
YZX
ZXY
ZYX

Algorithm

Step 1: given string.
Step 2: Get all permutations of string.
Step 3: print all permutations.

Example Code

from itertools import permutations
def allPermutations(str1):
   # Get all permutations of string 'ABC'
   per = permutations(str1)
   # print all permutations
   print("Permutation Of this String ::>")
   for i in list(per):
   print (''.join(i))
# Driver program
if __name__ == "__main__":
   str1 = input("Enter the string ::>")
allPermutations(str1)

Output

Enter the string ::> abc
Permutation Of this String ::>
abc
acb
bac
bca
cab
cba