---
title: "Implement a python program to generate all possible permutations of a given string."  
description: "Implement a python program to generate all possible permutations of a given string."  
author: "Steilla Mitchel"  
published: 2023-06-18  
updated: 2023-06-19  
canonical: https://www.mindstick.com/forum/158766/implement-a-python-program-to-generate-all-possible-permutations-of-a-given-string  
category: "python"  
tags: ["string", "python"]  
reading_time: 2 minutes  

---

# Implement a python program to generate all possible permutations of a given string.

Implement a [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) to [generate all possible permutations](https://www.mindstick.com/forum/158802/implement-a-rust-program-to-generate-all-possible-permutations-of-a-given-string) of a given string.

## Replies

### Reply by Aryan Kumar

Sure, here is a Python program to generate all possible [permutations](https://www.mindstick.com/interview/34418/random-permutations-using-numpy) of a given string:

Python

```plaintext
def permutations(string):
  """
  Generates all possible permutations of a given string.

  Args:
    string: The string to generate permutations for.

  Returns:
    A list of all possible permutations of the string.
  """

  if len(string) == 0:
    return ['']

  permutations_list = []
  for i in range(len(string)):
    first_character = string[i]
    remaining_string = string[:i] + string[i + 1:]
    permutations_for_remaining_string = permutations(remaining_string)
    for permutation in permutations_for_remaining_string:
      permutations_list.append(first_character + permutation)

  return permutations_list

if __name__ == "__main__":
  string = "abc"
  permutations_list = permutations(string)
  print(permutations_list)
```

This program works by first checking if the string is empty. If the string is empty, then the program returns an empty list.

Otherwise, the program iterates through the characters in the string and calls the `permutations()` function recursively. The `permutations()` function recursively generates all possible permutations of the remaining string. The program then appends each permutation to the `permutations_list`.

Finally, the program returns the `permutations_list`.

To run the program, you can save it as a Python file and then run it from the command line. For example, if you save the program as `permutations.py`, you can run it by typing the following command into the command line:

Code snippet

```plaintext
python permutations.py
```

This will print the list of all possible permutations of the string to the console.

Here is an example of the output of the program:

Code snippet

```plaintext
$ python permutations.py
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
```

As you can see, the output of the program is a list of all possible permutations of the string "abc", which is a total of 6 permutations.


---

Original Source: https://www.mindstick.com/forum/158766/implement-a-python-program-to-generate-all-possible-permutations-of-a-given-string

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
