Find all permutations of a given string in Python
In this example, we will see a Python program through which we can find all the possible permutation values of a given string.
For solving this problem, we will use "itertools" module of Python which is basically a collection of tools for handling iterators.
Algorithm:
- STEP 1: Initially, we will import the permutation function from the itertools module.
- STEP 2: Take the string from the user and assign it in a variable s.
- STEP 3: Generate all permutation using the permutation function and assign it in a variable p.
- STEP 4: Since all elements of p are in tuple form. So, convert it in the list.
- STEP 5: At last, add all list elements and print it which is our possible permutations.
Program:
# importing the module
from itertools import permutations
# input the sting
s=input('Enter a string: ')
A=[]
b=[]
p=permutations(s)
for k in list(p):
A.append(list(k))
for j in A:
r=''.join(str(l) for l in j)
b.append(r)
print('Number of all permutations: ',len(b))
print('All permutations are: ')
print(b)
Output:
Enter a string: abcd
Number of all permutations: 300
All permutations are:
['abcd', 'abcd', 'abdc', 'abcd', 'abdc', 'acbd', 'abcd', 'abdc', 'acbd', 'acdb',
'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'abcd', 'abdc', 'acbd', 'acdb', 'adbc',
'adcb', 'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', 'bacd', 'abcd', 'abdc',
'acbd', 'acdb', 'adbc', 'adcb', 'bacd', 'badc', 'abcd', 'abdc', 'acbd', 'acdb',
'adbc', 'adcb', 'bacd', 'badc', 'bcad', 'abcd', 'abdc', 'acbd', 'acdb', 'adbc',
'adcb', 'bacd', 'badc', 'bcad', 'bcda', 'abcd', 'abdc', 'acbd', 'acdb', 'adbc',
'adcb', 'bacd', 'badc', 'bcad', 'bcda', 'bdac', 'abcd', 'abdc', 'acbd', 'acdb',
'adbc', 'adcb', 'bacd', 'badc', 'bcad', 'bcda', 'bdac', 'bdca', 'abcd', 'abdc',
'acbd', 'acdb', 'adbc', 'adcb', 'bacd', 'badc', 'bcad', 'bcda', 'bdac', 'bdca',
'cabd', 'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', 'bacd', 'badc', 'bcad',
'bcda', 'bdac', 'bdca', 'cabd', 'cadb', 'abcd', 'abdc', 'acbd', 'acdb', 'adbc',
'adcb', 'bacd', 'badc', 'bcad', 'bcda', 'bdac', 'bdca', 'cabd', 'cadb', 'cbad',
'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', 'bacd', 'badc', 'bcad', 'bcda',
'bdac', 'bdca', 'cabd', 'cadb', 'cbad', 'cbda', 'abcd', 'abdc', 'acbd', 'acdb',
'adbc', 'adcb', 'bacd', 'badc', 'bcad', 'bcda', 'bdac', 'bdca', 'cabd', 'cadb',
'cbad', 'cbda', 'cdab', 'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', 'bacd',
'badc', 'bcad', 'bcda', 'bdac', 'bdca', 'cabd', 'cadb', 'cbad', 'cbda', 'cdab',
'cdba', 'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', 'bacd', 'badc', 'bcad',
'bcda', 'bdac', 'bdca', 'cabd', 'cadb', 'cbad', 'cbda', 'cdab', 'cdba', 'dabc',
'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', 'bacd', 'badc', 'bcad', 'bcda',
'bdac', 'bdca', 'cabd', 'cadb', 'cbad', 'cbda', 'cdab', 'cdba', 'dabc', 'dacb',
'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', 'bacd', 'badc', 'bcad', 'bcda',
'bdac', 'bdca', 'cabd', 'cadb', 'cbad', 'cbda', 'cdab', 'cdba', 'dabc', 'dacb',
'dbac', 'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', 'bacd', 'badc', 'bcad',
'bcda', 'bdac', 'bdca', 'cabd', 'cadb', 'cbad', 'cbda', 'cdab', 'cdba', 'dabc',
'dacb', 'dbac', 'dbca', 'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', 'bacd',
'badc', 'bcad', 'bcda', 'bdac', 'bdca', 'cabd', 'cadb', 'cbad', 'cbda', 'cdab',
'cdba', 'dabc', 'dacb', 'dbac', 'dbca', 'dcab', 'abcd', 'abdc', 'acbd', 'acdb',
'adbc', 'adcb', 'bacd', 'badc', 'bcad', 'bcda', 'bdac', 'bdca', 'cabd', 'cadb',
'cbad', 'cbda', 'cdab', 'cdba', 'dabc', 'dacb', 'dbac', 'dbca', 'dcab', 'dcba']