How to Repeat a String in Python? Last Updated : 12 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Repeating a string is a simple task in Python. We can create multiple copies of a string by using built-in features. This is useful when we need to repeat a word, phrase, or any other string a specific number of times. Using Multiplication Operator (*):Using Multiplication operator (*) is the simplest and most efficient way to repeat a string in Python. It directly repeats the string for a specified number of times. Python s = "Hello! " # Repeat the string 3 times r= s * 3 print(r) OutputHello! Hello! Hello! Let's take a look on several ways to repeat string in python:Table of ContentUsing itertools.repeat()Using for LoopUsing numpy.repeat()Using itertools.repeat()This function in Python allows us to create an iterator that repeats a value multiple times. It's useful when we need a sequence of string for a specific number of iterations. Python import itertools # Repeating a string using itertools.repeat s = ''.join(itertools.repeat("Hello! ", 3)) print(s) OutputHello! Hello! Hello! Explanation:itertools.repeat("Hello! ", 3): This generates an iterator that repeats the string "Hello! " exactly 3 times.''.join(...): This method merges the repeated strings into one continuous string, resulting in "Hello! Hello! Hello! ".Using for LoopRepeating a string using a for loop is a more flexible method. You iterate a specified number of times and append the string to a variable each time. Python s = "" # for loop to repeat a string for _ in range(4): s += "Hello!" print(s) OutputHello!Hello!Hello!Hello! Using numpy.repeat()We can also repeat a string using numpy.repeat(). This method repeats the string a specified number of times and returns the result as a array, which can be easily converted into a single string. Python import numpy as np # Repeating the string s = np.repeat("Hello! ", 3) # Joining array into a single string result = ''.join(s) print(result) OutputHello! Hello! Hello! Expalnation:np.repeat("Hello! ", 3): This repeats the string "Hello! " 3 times and stores it in a numpy array.''.join(s): This repeated elements in the array into one continuous string, resulting in "Hello! Hello! Hello! " Comment More infoAdvertise with us Next Article How to Repeat a String in Python? V vishakshx339 Follow Improve Article Tags : Python Python Programs python-string Python string-programs python +1 More Practice Tags : pythonpython Similar Reads How to Remove a Substring in Python? In Python, removing a substring from a string can be achieved through various methods such as using replace() function, slicing, or regular expressions. Depending on your specific use case, you may want to remove all instances of a substring or just the first occurrence. Letâs explore different ways 2 min read How to copy a string in Python Creating a copy of a string is useful when we need a duplicate of a string to work with while keeping the original string intact, strings in Python are immutable which means they can't be altered after creation, so creating a copy sometimes becomes a necessity for specific use cases.Using SlicingSli 2 min read How to Count Repeated Words in a String in Python In this article, we will learn how to count repeated words in a string. Python provides several methods to Count Repeated Words , such as dictionaries, collections. Counter module, or even regular expressions. The simplest way to count repeated words is by splitting the string into individual words 2 min read Python | Repeat String till K Sometimes, while working with strings, we might encounter a use case in which we need to repeat our string to the size of K, even though the last string might not be complete, but has to stop as the size of string becomes K. The problem of repeating string K times, is comparatively simpler than this 4 min read How to Append to String in Python ? In Python, Strings are immutable datatypes. So, appending to a string is nothing but string concatenation which means adding a string at the end of another string.Let us explore how we can append to a String with a simple example in Python.Pythons = "Geeks" + "ForGeeks" print(s)OutputGeeksForGeeks N 2 min read Like