Python | Permutation of a given string using inbuilt function Last Updated : 30 Dec, 2024 Comments Improve Suggest changes Like Article Like Report The task is to generate all the possible permutations of a given string. A permutation of a string is a rearrangement of its characters. For example, the permutations of "ABC" are "ABC", "ACB", "BAC", "BCA", "CAB", and "CBA". The number of permutations of a string with n unique characters is n! (factorial of n).We can generate permutations of a string in Python using methods like inbuilt functions and recursion. Using itertools.permutations itertools module in Python provides a simple function called permutations that can generate all possible permutations of a string. This method is the easiest and most efficient, especially when working with built-in Python libraries. Python import itertools s = "GFG" li = [''.join(p) for p in itertools.permutations(s)] print(li) Explanation:itertools.permutations(s) generates all possible permutations of the string s, returning each permutation as a tuple.''.join(p) converts each tuple p into a string by concatenating its elements.list comprehension collects all the joined strings (permutations) into a list.Using Recursion Recursion is a method where a function calls itself to solve smaller parts of a problem. In the case of generating permutations, we can break down the problem by selecting one character at a time and recursively permuting the rest of the string. This approach is a bit more complex than using itertools.permutations, but it can be useful if we want more control over the process. Python def permute(s, s2): if len(s) == 0: print(s2, end=' ') return for i in range(len(s)): char = s[i] left_s = s[0:i] right_s = s[i+1:] rest = left_s + right_s permute(rest, s2 + char) s1 = "GFG" s2 = "" permute(s1, s2) OutputGFG GGF FGG FGG GGF GFG Explanation:If the string s is empty, print the accumulated answer.For each character in s, remove the character and recursively permute the remaining characters while adding the character to answer.Call permute(s, answer) with the initial string and an empty answer to start generating permutations. Comment More infoAdvertise with us Next Article Must Coding Questions - Company-wise S Shashank Mishra Follow Improve Article Tags : Python Python-Built-in-functions python-string Python string-programs Practice Tags : python Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like