How to Repeat a String in Python

In this tutorial we are going to show you How to Repeat a String in Python.

So, we are going to write a program that captures text from the user in python. Then print the user string as many times as its length. Print as many lines of this as the length of the user input. Important, do not put a prompt when asking for user input. Just use input(). Adding a prompt will cause your program to not pass the tests.

Expected Output

• If the user types cat then the program will print: 
catcatcat
catcatcat
catcatcat 

Implementation of How to Repeat a String in Python

#Solution by aiinspired.com

#Get the user input and store it in the variable string
string = input()

#Using len() function get the length of the string
length = len(string) 

#Using for loop to repeat number of times equal to length
for x in range(length):
     for x in range(length):
         print(string, end ="")
     print()

#save the file as ex1.py

Download Sourcecode Here

The above code will produce the output as below

How to Repeat a String in Python
Output of How to Repeat a String in Python

Instructions on how to run the source code

You can use online python interpreter if you haven’t configured python on your computer.

To execute the above code, Open CMD and make sure you are in the directory where your file is. Then run the below code

python ex1.py

LEAVE A REPLY

Please enter your comment!
Please enter your name here