Home » Python » Python programs

# Find all permutations of a given string in Python

**Finding all permutations of a given string**: Here, we are going to learn **how to find all permutations for a given string by using the itertools module** in Python programming language?

Submitted by Bipin Kumar, on November 11, 2019

**Python itertools Module**

**"itertools"** are an inbuilt module in Python which is a collection of tools for handling iterators. It is the most useful module of Python. Here, a string is provided by the user and we have to **print all the possible permutations of the given string in Python**. As we all know the permutation is a way of arranging the elements of a group or set in a specific order or sequence which makes a different group. We all have calculated the permutations by using the pen and paper but here we will do this by using the Python programming language in a very small time.

**Algorithm to solve the problem:**

- Initially, we will import the permutation function from the itertools module.
- Take the string from the user and assign it in a variable s.
- Generate all permutation using the permutation function and assign it in a variable p.
- Since all elements of p are in tuple form. So, convert it in the list.
- At last, add all list elements and print it which is our possible permutations.

Let's start writing the Python program by implementing the above algorithm in a simple way.

### Python program to find all permutations 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']

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.