
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Permutation of a given string using the inbuilt function in Python
In this tutorial, we are going to find the permutation of a string using the inbuilt function of Python called permutations. The method permutations is present in the itertools module.
Procedure To Find The Permutation Of A String
- Import the itertools module.
- Initialize the string.
- Use the itertools.permutations method to find the permutation of the string.
- In the third step, the method returns an object and convert it into a list.
- List contains a permutation of string as tuples.
Example
Let's see the program.
## importing the module import itertools ## initializing a string string = "XYZ" ## itertools.permutations method permutaion_list = list(itertools.permutations(string)) ## printing the obj in list print("-----------Permutations Of String In Tuples----------------") print(permutaion_list) ## converting the tuples to string using 'join' method print("-------------Permutations In String Format-----------------") for tup in permutaion_list: print("".join(tup))
Output
If you run the above program, you will get the following result.
-----------Permutations Of String In Tuples---------------- [('X', 'Y', 'Z'), ('X', 'Z', 'Y'), ('Y', 'X', 'Z'), ('Y', 'Z', 'X'), ('Z', 'X', 'Y'), ('Z', 'Y', 'X')] -------------Permutations In String Format----------------- XYZ XZY YXZ YZX ZXY ZYX
If you have any doubts regarding the program, please do mention them in the comment section.
- Related Articles
- Python program for permutation of a given string inbuilt function in python
- Python program for the permutation of a given string inbuilt function in python
- Print all permutation of a string using ArrayList in Java
- Golang Program to reverse the elements of the array using inbuilt function
- Rearrange positive and negative numbers using inbuilt sort function in C++
- Inbuilt Data Structures in Python
- How to find all the permutation of the string by backtracking using C#?
- Permutation in String in C++
- Python program to count the number of vowels using set in a given string
- Find all the patterns of "10+1" in a given string using Python Regex
- Python program to count the number of vowels using sets in a given string
- Python Program to Calculate the Length of a String Without Using a Library Function
- Python program to change character of a string using given index
- Generate all permutation of a set in Python?
- Find all the patterns of “1(0+)1” in a given string using Python Regex

Advertisements