
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Python program to get all permutations of size r of a string
Suppose we have a string s and a number r. We have to display all permutations of r number of characters in s. We have permutations() function to get all permutations. This function is present inside itertools library.
So, if the input is like s = "HELLO" r = 3, then the output will be
>['HEL', 'HEL', 'HEO', 'HLE', 'HLL', 'HLO', 'HLE', 'HLL', 'HLO', 'HOE', 'HOL', 'HOL', 'EHL', 'EHL', 'EHO', 'ELH', 'ELL', 'ELO', 'ELH', 'ELL','ELO', 'EOH', 'EOL', 'EOL', 'LHE', 'LHL', 'LHO', 'LEH', 'LEL', 'LEO', 'LLH', 'LLE', 'LLO', 'LOH', 'LOE', 'LOL', 'LHE', 'LHL', 'LHO', 'LEH', 'LEL', 'LEO', 'LLH', 'LLE', 'LLO', 'LOH', 'LOE', 'LOL', 'OHE', 'OHL', 'OHL', 'OEH', 'OEL', 'OEL', 'OLH', 'OLE', 'OLL', 'OLH', 'OLE', 'OLL']
To solve this, we will follow these steps −
- vals:= a list with all permutations of size r from s
- res:= a new list
- for each x in vals, do
- convert list of characters x into string and insert into res
- return res
Example
Let us see the following implementation to get better understanding
from itertools import permutations def solve(s, r): vals=list(permutations(s,r)) res=[] for x in vals: res.append(''.join(x)) return res s = "HELLO" r = 3 print(solve(s, r))
Input
"HELLO", 2
Output
['HEL', 'HEL', 'HEO', 'HLE', 'HLL', 'HLO', 'HLE', 'HLL', 'HLO', 'HOE', 'HOL', 'HOL', 'EHL', 'EHL', 'EHO', 'ELH', 'ELL', 'ELO', 'ELH', 'ELL', 'ELO', 'EOH', 'EOL', 'EOL', 'LHE', 'LHL', 'LHO', 'LEH', 'LEL', 'LEO', 'LLH', 'LLE', 'LLO', 'LOH', 'LOE', 'LOL', 'LHE', 'LHL', 'LHO', 'LEH', 'LEL', 'LEO', 'LLH', 'LLE', 'LLO', 'LOH', 'LOE', 'LOL', 'OHE', 'OHL', 'OHL', 'OEH', 'OEL', 'OEL', 'OLH', 'OLE', 'OLL', 'OLH', 'OLE', 'OLL']
- Related Articles
- Python Program to print all permutations of a given string
- C Program to print all permutations of a given string
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion
- Python Program to Print All Permutations of a String in Lexicographic Order using Recursion
- Python program to get all subsets of given size of a set
- Python program to get all subsets of a given size of a set
- Golang program to compute all the permutations of the string
- How to find all possible permutations of a given string in Python?
- Print all permutations of a given string
- All permutations of a string using iteration?
- Print all permutations of a string in Java
- How to get the size of a string in Python?
- Java Program to print distinct permutations of a string
- Program to count average of all special values for all permutations of a list of items in Python
- Print all palindrome permutations of a string in C++

Advertisements