Python Program to print all permutations of a given string; In this python tutorial, we would like to share with you two ways to find and print permutations of given string in python by using built-in module and without using any module.
Before we share with you program to find all permutations of the given string in python. You should know about the python itertools modules, because this module will help to find all permutations of given string.
permutation:- As we all know, the permutation is a way of organizing the elements of a group or set in a specific order or sequence that forms a separate group.
Python Program to print all permutations of a given string
- 1: Find all permutations of a string in Python
- 2: Python Program to Print all Permutations of a Given String without using Built-in Function
1: Find all permutations of a string in Python
Use the folloiwng steps and write a python program to print all permutations of a given string:
- First of all, import the permutation function from the python itertools module in program.
- Allow user to input the string and assign it in a variable.
- Use the permutation function to find all permutations and assign it in a variable.
- Since all elements are in tuple form. So, convert it in the list.
- At end of program, Print it which is our possible permutations.
# import the module from itertools import permutations # input the sting str=input('Enter a string: ') A=[] b=[] p=permutations(str) 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)
After executing the program, the output will be:
Enter a string: cba Number of all permutations: 21 All permutations are: ['cba', 'cba', 'cab', 'cba', 'cab', 'bca', 'cba', 'cab', 'bca', 'bac', 'cba', 'cab', 'bca', 'bac', 'acb', 'cba', 'cab', 'bca', 'bac', 'acb', 'abc']
2: Python Program to Print all Permutations of a Given String without using Built-in Function
# conversion def toString(List): return ''.join(List) # find all permutations def permuteFunc(a, l, r): if l == r: print (toString(a)) else: for i in range(l, r + 1): a[l], a[i] = a[i], a[l] permuteFunc(a, l + 1, r) a[l], a[i] = a[i], a[l] # backtracking # main str=input('Enter a string: ') n = len(str) a = list(str) print("The possible permutations are:",end="\n") permuteFunc(a, 0, n-1)
After executing the program, the output will be:
Enter a string: abc The possible permutations are: abc acb bac bca cba cab
Recommended Python String Programs
- Python Program to Convert Uppercase to Lowercase
- Convert String Lowercase to Uppercase in Python
- Python First Character of String Uppercase
- Python Concatenate String and Variable (int, float, etc)
- How to Find Length of String in Python
- Python: String Join Function
- Python String Append Examples
- How to replace a character in a string in python
- Python – Count Number of Occurrences in String
- Python Remove Last Character from String
- Python Remove Character From String
- Python Program to Reverse String
- Python Program to Remove First Occurrence of Character in a String
- Python: Remove Special Characters from String
- Python Program Count vowels in String
- Python Split a String into Array of Characters, Space
- Python Program to Swap Two Character of Given String
- Python Program String Find
- Python: Two Given Strings and Swap First Two Characters of Each String
- Python: Convert String to Float & Float to String
- Python Program to Reverse String Using Stack
- How To Convert Python Int to String and String to Int
- Python Convert String to List and List to String