In this python function program, we will get all permutations of a string using a string.
Permutation:
A permutation is an arrangement of objects in a definite order. It is a way of changing or arranging the elements or objects in a linear order.
What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.
Steps to solve the program
- Import itertools
- Create a function permutations
- Use the def keyword to define the function.
- Take a string as input through the user.
- Convert the string to the list using list().
- Find the permutaions i.e. different possible combinations of characters in the list using itertools.permutations(list1).
- Convert the result back to the list using the list.
return the output. - Call the function to get the output.
import itertools
def permutations():
string = input("Enter string: ")
list1 = list(string)
permutations = list(itertools.permutations(list1))
return permutations
permutations()
Output :
Enter string: name
[('n', 'a', 'm', 'e'),
('n', 'a', 'e', 'm'),
('n', 'm', 'a', 'e'),
('n', 'm', 'e', 'a'),
('n', 'e', 'a', 'm'),
('n', 'e', 'm', 'a'),
('a', 'n', 'm', 'e'),
('a', 'n', 'e', 'm'),
('a', 'm', 'n', 'e'),
('a', 'm', 'e', 'n'),
('a', 'e', 'n', 'm'),
('a', 'e', 'm', 'n'),
('m', 'n', 'a', 'e'),
('m', 'n', 'e', 'a'),
('m', 'a', 'n', 'e'),
('m', 'a', 'e', 'n'),
('m', 'e', 'n', 'a'),
('m', 'e', 'a', 'n'),
('e', 'n', 'a', 'm'),
('e', 'n', 'm', 'a'),
('e', 'a', 'n', 'm'),
('e', 'a', 'm', 'n'),
('e', 'm', 'n', 'a'),
('e', 'm', 'a', 'n')]
Related Articles
Python function program to add two numbers.
Python function program to print the input string 10 times.
Python function program to print a table of a given number.
Python function program to find the maximum of three numbers.
Python function program to find the sum of all the numbers in a list.
Python function program to multiply all the numbers in a list.
convert an integer to its word format.