Program to get all permutations of a string using a string

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
  1. Import itertools
  2. Create a function permutations
  3. Use the def keyword to define the function.
  4. Take a string as input through the user.
  5. Convert the string to the list using list().
  6. Find the permutaions i.e. different possible combinations of characters in the list using itertools.permutations(list1).
  7. Convert the result back to the list using the list.
    return the output.
  8. 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

convert an integer to its word format.

Leave a Comment