Python Program to print all permutations of a given string

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

  1. Python Program to Convert Uppercase to Lowercase
  2. Convert String Lowercase to Uppercase in Python
  3. Python First Character of String Uppercase
  4. Python Concatenate String and Variable (int, float, etc)
  5. How to Find Length of String in Python
  6. Python: String Join Function
  7. Python String Append Examples
  8. How to replace a character in a string in python
  9. Python – Count Number of Occurrences in String
  10. Python Remove Last Character from String
  11. Python Remove Character From String
  12. Python Program to Reverse String
  13. Python Program to Remove First Occurrence of Character in a String
  14. Python: Remove Special Characters from String
  15. Python Program Count vowels in String
  16. Python Split a String into Array of Characters, Space
  17. Python Program to Swap Two Character of Given String
  18. Python Program String Find
  19. Python: Two Given Strings and Swap First Two Characters of Each String
  20. Python: Convert String to Float & Float to String
  21. Python Program to Reverse String Using Stack
  22. How To Convert Python Int to String and String to Int
  23. Python Convert String to List and List to String

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *