Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Python: Print all permutations with given repetition number of characters of a given string

Python String: Exercise-52 with Solution

Write a Python program to print all permutations with given repetition number of characters of a given string.

Sample Solution:-

Python Code:

from itertools import product
def all_repeat(str1, rno):
  chars = list(str1)
  results = []
  for c in product(chars, repeat = rno):
    results.append(c)
  return results
print(all_repeat('xyz', 3))
print(all_repeat('xyz', 2))
print(all_repeat('abcd', 4))

Sample Output:

[('x', 'x', 'x'), ('x', 'x', 'y'), ('x', 'x', 'z'), ('x', 'y', 'x'), ('x', 'y', 'y'), ('x', 'y', 'z'), ('x', 'z', 'x'), ('x', 'z', 'y'), ('x', 'z', 'z'), ('y', 'x', 'x'), ('y', 'x', 'y'), ('y', 'x', 'z'), ('y', 'y', 'x'), ('y', 'y', 'y'), ('y', 'y', 'z'), ('y', 'z', 'x'), ('y', 'z', 'y'), ('y', 'z', 'z'), ('z', 'x', 'x'), ('z', 'x', 'y'), ('z', 'x', 'z'), ('z', 'y', 'x'), ('z', 'y', 'y'), ('z', 'y', 'z'), ('z', 'z', 'x'), ('z', 'z', 'y'), ('z', 'z', 'z')]
[('x', 'x'), ('x', 'y'), ('x', 'z'), ('y', 'x'), ('y', 'y'), ('y', 'z'), ('z', 'x'), ('z', 'y'), ('z', 'z')]
[('a', 'a', 'a', 'a'), ('a', 'a', 'a', 'b'), ('a', 'a', 'a', 'c'), ('a', 'a', 'a', 'd'), ('a', 'a', 'b', 'a'), ('a', 'a', 'b', 'b'), ('a', 'a', 'b', 'c'), ('a', 'a', 'b', 'd'), ('a', 'a', 'c', 'a'), ('a', 'a', 'c', 'b'), ('a', 'a', 'c', 'c'), ('a', 'a', 'c', 'd'), ('a', 'a', 'd', 'a'), ('a', 'a', 'd', 'b'), ('a', 'a', 'd', 'c'), ('a', 'a', 'd', 'd'), ('a', 'b', 'a', 'a'), ('a', 'b', 'a', 'b'), ('a', 'b', 'a', 'c'), ('a', 'b', 'a', 'd'), ('a', 'b', 'b', 'a'), ('a', 'b', 'b', 'b'), ('a', 'b', 'b', 'c'), ('a', 'b', 'b', 'd'), ('a', 'b', 'c', 'a'), ('a', 'b', 'c', 'b'), ('a', 'b', 'c', 'c'), ('a', 'b', 'c', 'd'), ('a', 'b', 'd', 'a'), ('a', 'b', 'd', 'b'), ('a', 'b', 'd', 'c'), ('a', 'b', 'd', 'd'), ('a', 'c', 'a', 'a'), ('a', 'c', 'a', 'b'), ('a', 'c', 'a', 'c'), ('a', 'c', 'a', 'd'), ('a', 'c', 'b', 'a'), ('a', 'c', 'b', 'b'), ('a', 'c', 'b', 'c'), ('a', 'c', 'b', 'd'), ('a', 'c', 'c', 'a'), ('a', 'c', 'c', 'b'), ('a', 'c', 'c', 'c'), ('a', 'c', 'c', 'd'), ('a', 'c', 'd', 'a'), ('a', 'c', 'd', 'b'), ('a', 'c', 'd', 'c'), ('a', 'c', 'd', 'd'), ('a', 'd', 'a', 'a'), ('a', 'd', 'a', 'b'), ('a', 'd', 'a', 'c'), ('a', 'd', 'a', 'd'), ('a', 'd', 'b', 'a'), ('a', 'd', 'b', 'b'), ('a', 'd', 'b', 'c'), ('a', 'd', 'b', 'd'), ('a', 'd', 'c', 'a'), ('a', 'd', 'c', 'b'), ('a', 'd', 'c', 'c'), ('a', 'd', 'c', 'd'), ('a', 'd', 'd', 'a'), ('a', 'd', 'd', 'b'), ('a', 'd', 'd', 'c'), ('a', 'd', 'd', 'd'), ('b', 'a', 'a', 'a'), ('b', 'a', 'a', 'b'), ('b', 'a', 'a', 'c'), ('b', 'a', 'a', 'd'), ('b', 'a', 'b', 'a'), ('b', 'a', 'b', 'b'), ('b', 'a', 'b', 'c'), ('b', 'a', 'b', 'd'), ('b', 'a', 'c', 'a'), ('b', 'a', 'c', 'b'), ('b', 'a', 'c', 'c'), ('b', 'a', 'c', 'd'), ('b', 'a', 'd', 'a'), ('b', 'a', 'd', 'b'), ('b', 'a', 'd', 'c'), ('b', 'a', 'd', 'd'), ('b', 'b', 'a', 'a'), ('b', 'b', 'a', 'b'), ('b', 'b', 'a', 'c'), ('b', 'b', 'a', 'd'), ('b', 'b', 'b', 'a'), ('b', 'b', 'b', 'b'), ('b', 'b', 'b', 'c'), ('b', 'b', 'b', 'd'), ('b', 'b', 'c', 'a'), ('b', 'b', 'c', 'b'), ('b', 'b', 'c', 'c'), ('b', 'b', 'c', 'd'), ('b', 'b', 'd', 'a'), ('b', 'b', 'd', 'b'), ('b', 'b', 'd', 'c'), ('b', 'b', 'd', 'd'), ('b', 'c', 'a', 'a'), ('b', 'c', 'a', 'b'), ('b', 'c', 'a', 'c'), ('b', 'c', 'a', 'd'), ('b', 'c', 'b', 'a'), ('b', 'c', 'b', 'b'), ('b', 'c', 'b', 'c'), ('b', 'c', 'b', 'd'), ('b', 'c', 'c', 'a'), ('b', 'c', 'c', 'b'), ('b', 'c', 'c', 'c'), ('b', 'c', 'c', 'd'), ('b', 'c', 'd', 'a'), ('b', 'c', 'd', 'b'), ('b', 'c', 'd', 'c'), ('b', 'c', 'd', 'd'), ('b', 'd', 'a', 'a'), ('b', 'd', 'a', 'b'), ('b', 'd', 'a', 'c'), ('b', 'd', 'a', 'd'), ('b', 'd', 'b', 'a'), ('b', 'd', 'b', 'b'), ('b', 'd', 'b', 'c'), ('b', 'd', 'b', 'd'), ('b', 'd', 'c', 'a'), ('b', 'd', 'c', 'b'), ('b', 'd', 'c', 'c'), ('b', 'd', 'c', 'd'), ('b', 'd', 'd', 'a'), ('b', 'd', 'd', 'b'), ('b', 'd', 'd', 'c'), ('b', 'd', 'd', 'd'), ('c', 'a', 'a', 'a'), ('c', 'a', 'a', 'b'), ('c', 'a', 'a', 'c'), ('c', 'a', 'a', 'd'), ('c', 'a', 'b', 'a'), ('c', 'a', 'b', 'b'), ('c', 'a', 'b', 'c'), ('c', 'a', 'b', 'd'), ('c', 'a', 'c', 'a'), ('c', 'a', 'c', 'b'), ('c', 'a', 'c', 'c'), ('c', 'a', 'c', 'd'), ('c', 'a', 'd', 'a'), ('c', 'a', 'd', 'b'), ('c', 'a', 'd', 'c'), ('c', 'a', 'd', 'd'), ('c', 'b', 'a', 'a'), ('c', 'b', 'a', 'b'), ('c', 'b', 'a', 'c'), ('c', 'b', 'a', 'd'), ('c', 'b', 'b', 'a'), ('c', 'b', 'b', 'b'), ('c', 'b', 'b', 'c'), ('c', 'b', 'b', 'd'), ('c', 'b', 'c', 'a'), ('c', 'b', 'c', 'b'), ('c', 'b', 'c', 'c'), ('c', 'b', 'c', 'd'), ('c', 'b', 'd', 'a'), ('c', 'b', 'd', 'b'), ('c', 'b', 'd', 'c'), ('c', 'b', 'd', 'd'), ('c', 'c', 'a', 'a'), ('c', 'c', 'a', 'b'), ('c', 'c', 'a', 'c'), ('c', 'c', 'a', 'd'), ('c', 'c', 'b', 'a'), ('c', 'c', 'b', 'b'), ('c', 'c', 'b', 'c'), ('c', 'c', 'b', 'd'), ('c', 'c', 'c', 'a'), ('c', 'c', 'c', 'b'), ('c', 'c', 'c', 'c'), ('c', 'c', 'c', 'd'), ('c', 'c', 'd', 'a'), ('c', 'c', 'd', 'b'), ('c', 'c', 'd', 'c'), ('c', 'c', 'd', 'd'), ('c', 'd', 'a', 'a'), ('c', 'd', 'a', 'b'), ('c', 'd', 'a', 'c'), ('c', 'd', 'a', 'd'), ('c', 'd', 'b', 'a'), ('c', 'd', 'b', 'b'), ('c', 'd', 'b', 'c'), ('c', 'd', 'b', 'd'), ('c', 'd', 'c', 'a'), ('c', 'd', 'c', 'b'), ('c', 'd', 'c', 'c'), ('c', 'd', 'c', 'd'), ('c', 'd', 'd', 'a'), ('c', 'd', 'd', 'b'), ('c', 'd', 'd', 'c'), ('c', 'd', 'd', 'd'), ('d', 'a', 'a', 'a'), ('d', 'a', 'a', 'b'), ('d', 'a', 'a', 'c'), ('d', 'a', 'a', 'd'), ('d', 'a', 'b', 'a'), ('d', 'a', 'b', 'b'), ('d', 'a', 'b', 'c'), ('d', 'a', 'b', 'd'), ('d', 'a', 'c', 'a'), ('d', 'a', 'c', 'b'), ('d', 'a', 'c', 'c'), ('d', 'a', 'c', 'd'), ('d', 'a', 'd', 'a'), ('d', 'a', 'd', 'b'), ('d', 'a', 'd', 'c'), ('d', 'a', 'd', 'd'), ('d', 'b', 'a', 'a'), ('d', 'b', 'a', 'b'), ('d', 'b', 'a', 'c'), ('d', 'b', 'a', 'd'), ('d', 'b', 'b', 'a'), ('d', 'b', 'b', 'b'), ('d', 'b', 'b', 'c'), ('d', 'b', 'b', 'd'), ('d', 'b', 'c', 'a'), ('d', 'b', 'c', 'b'), ('d', 'b', 'c', 'c'), ('d', 'b', 'c', 'd'), ('d', 'b', 'd', 'a'), ('d', 'b', 'd', 'b'), ('d', 'b', 'd', 'c'), ('d', 'b', 'd', 'd'), ('d', 'c', 'a', 'a'), ('d', 'c', 'a', 'b'), ('d', 'c', 'a', 'c'), ('d', 'c', 'a', 'd'), ('d', 'c', 'b', 'a'), ('d', 'c', 'b', 'b'), ('d', 'c', 'b', 'c'), ('d', 'c', 'b', 'd'), ('d', 'c', 'c', 'a'), ('d', 'c', 'c', 'b'), ('d', 'c', 'c', 'c'), ('d', 'c', 'c', 'd'), ('d', 'c', 'd', 'a'), ('d', 'c', 'd', 'b'), ('d', 'c', 'd', 'c'), ('d', 'c', 'd', 'd'), ('d', 'd', 'a', 'a'), ('d', 'd', 'a', 'b'), ('d', 'd', 'a', 'c'), ('d', 'd', 'a', 'd'), ('d', 'd', 'b', 'a'), ('d', 'd', 'b', 'b'), ('d', 'd', 'b', 'c'), ('d', 'd', 'b', 'd'), ('d', 'd', 'c', 'a'), ('d', 'd', 'c', 'b'), ('d', 'd', 'c', 'c'), ('d', 'd', 'c', 'd'), ('d', 'd', 'd', 'a'), ('d', 'd', 'd', 'b'), ('d', 'd', 'd', 'c'), ('d', 'd', 'd', 'd')]          

Pictorial Presentation:

Python String: Print all permutations with given repetition number of characters of a given string

Flowchart:

Flowchart: Print all permutations with given repetition number of characters of a given string

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the first non-repeating character in given string.
Next:Write a Python program to find the first repeated character in a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m