How to Create a String of Specified Length in Python Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Creating a string of a specific length in Python can be done using various methods, depending on the type of string and its contents. Let’s explore the most common techniques to achieve this.The simplest way to create a string of a specified length is by multiplying a character or sequence of characters. Python # Create a string of 10 asterisks s1 = '*' * 10 print(s1) # Create a string of 20 dashes s2 = '-' * 20 print(s2) Output********** -------------------- This approach is efficient and straightforward for creating repeated patterns.Let's explore other methods of creating a string of specified length in python:Using ljust Method (For Padding/Alignment Purpose)The ljust() method allows you to pad a string to a specified length with a default or custom character. Python # Create a string of 15 spaces s1 = "".ljust(15) print(f"'{s1}'") # Create a string of 10 underscores s2 = "".ljust(10, '_') print(s2) ljust() method is useful when you need padding for alignment purposes.Using List Comprehension and join() This method creates list of repeated characters and join them into string. We can use this method for additional logic while creating string. Python length = 10 char = 'a' result = ''.join([char for _ in range(length)]) print(result) Note: This method is less efficient than * operator (Method 1). Using str.zfill() Method (For 0 Padding)If you want to create a string of a specific length consisting of zeros, the zfill() method is ideal. Python # Create a string of 8 zeros s1 = "0".zfill(8) print(s1) Output00000000 This is particularly useful when dealing with numeric strings. Comment More infoAdvertise with us Next Article Company-wise Practice Problems A anuragtriarna Follow Improve Article Tags : Python Python Programs 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