Find all permutations of a given string in Python

Discovering all stages of a given string: Here, we will figure out how to discover all changes for a given string by utilizing the itertools module in Python programming language?

Python itertools Module

itertools” are an inbuilt module in Python which is an assortment of apparatuses for dealing with iterators. It is the most valuable module of Python.

Here, a string is given by the client and we need to print all the potential stages of the given string in Python. As we as a whole realize the stage is a method for organizing the components of a gathering or set in a particular request or succession which makes an alternate gathering.

We as a whole have determined the stages by utilizing the pen and paper yet here we will do this by utilizing the Python programming language in an exceptionally little league.

The calculation to take care of the issue:

  1. At first, we will import the stage work from the itertools module.
  2. Take the string from the client and appoint it in a variable s.
  3. Create all change utilizing the stage work and dole out it in a variable p.
  4. Since all components of p are in tuple structure. Thus, convert it in the rundown.
  5. Finally, include all rundown components and print it which is our potential changes.

How about we start composing the Python program by actualizing the above calculation in a basic manner.

Python program to discover all stages of a given string:

# 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: ABC
Number of all permutations: 21
All permutations are:
['ABC', 'ABC', 'ACB', 'ABC', 'ACB', 'BAC', 'ABC', 'ACB', 'BAC', 'BCA', 'ABC', 
'ACB', 'BAC', 'BCA', 'CAB', 'ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']

Leave a Comment

error: Alert: Content is protected!!