
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print All Permutations of a Given String in Python
In this article, we will learn about the solution to the problem statement given below.
Problem statement − We are given a string we need to display all the possible permutations of the string.
Now let’s observe the solution in the implementation below −
Example
# conversion def toString(List): return ''.join(List) # permutations def permute(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] permute(a, l + 1, r) a[l], a[i] = a[i], a[l] # backtracking # main string = "TUT" n = len(string) a = list(string) print("The possible permutations are:",end="\n") permute(a, 0, n-1)
Output
The possible permutations are: TUT TTU UTT UTT TUT TTU
All the variables are declared in the local scope and their references are seen in the figure above.
Conclusion
In this article, we have learned about how we can make a Python Program to print all permutations of a given string.
Advertisements