Join our newsletter for the latest updates.

Python Program to Compute all the Permutation of the String

In this example, you will learn to compute all the permutation of the string.

To understand this example, you should have the knowledge of the following Python programming topics:


Example 1: Using recursion

def get_permutation(string, i=0):

    if i == len(string):   	 
        print("".join(string))

    for j in range(i, len(string)):

        words = [c for c in string]
   
        # swap
        words[i], words[j] = words[j], words[i]
   	 
        get_permutation(words, i + 1)

print(get_permutation('yup'))

Output

yup
ypu
uyp
upy
puy
pyu
None

Example 2: Using itertools

from itertools import permutations

words = [''.join(p) for p in permutations('prog')]

print(words)

Output

Hello python