Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to copy a string in Python
Next article icon

How to Repeat a String in Python?

Last Updated : 12 Dec, 2024
Comments
Improve
Suggest changes
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) 

Output
Hello! Hello! Hello!  

Let's take a look on several ways to repeat string in python:

Table of Content

  • Using itertools.repeat()
  • Using for Loop
  • Using 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) 

Output
Hello! 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 Loop

Repeating 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) 

Output
Hello!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) 

Output
Hello! 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! "

Next Article
How to copy a string in Python

V

vishakshx339
Improve
Article Tags :
  • Python
  • Python Programs
  • python-string
  • Python string-programs
  • python
Practice Tags :
  • python
  • python

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 SlicingSl
    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. [GFGTABS] Python s = "Geeks" + "ForGeeks
    2 min read
  • How to Create String Array in Python ?
    To create a string array in Python, different methods can be used based on the requirement. A list can store multiple strings easily, NumPy arrays offer more features for large-scale data and the array module provides type-restricted storage. Each method helps in managing collections of text values
    2 min read
  • How to Initialize a String in Python
    In Python, initializing a string variable is straightforward and can be done in several ways. Strings in Python are immutable sequences of characters enclosed in either single quotes, double quotes or triple quotes. Let’s explore how to efficiently initialize string variables. Using Single or Double
    2 min read
  • Remove spaces from a string in Python
    Removing spaces from a string is a common task in Python that can be solved in multiple ways. For example, if we have a string like " g f g ", we might want the output to be "gfg" by removing all the spaces. Let's look at different methods to do so: Using replace() methodTo remove all spaces from a
    2 min read
  • Python - Check if string repeats itself
    Checking if a string repeats itself means determining whether the string consists of multiple repetitions of a smaller substring. Using slicing and multiplicationWe can check if the string is a repetition of a substring by slicing and reconstructing the string using multiplication. [GFGTABS] Python
    3 min read
  • Python | Add one string to another
    The concatenation of two strings has been discussed multiple times in various languages. But the task is how to add to a string in Python or append one string to another in Python. Example Input: 'GFG' + 'is best' Output: 'GFG is best' Explanation: Here we can add two string using "+" operator in Py
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences