• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Repeat String with * Operator in Python

Repeat String with * Operator in Python

February 18, 2022 Leave a Comment

In Python, we can easily repeat a string as many times as you would like. The easiest way to repeat a string n times is to use the Python * operator.

repeated_string = "string" * 3

print(repeated_string)

#Output:
stringstringstring

You can also repeat a string separated by a certain separator.

string = "string"
separator = "," 

repeated_string = (string + separator) * 3

print(repeated_string[:-1])

#Output:
string,string,string

When using string variables in Python, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is repeating a string n times. We can repeat strings with the * Python operator.

For example, if we want to repeat a string 3 times, we can just multiply the string by 3.

Below is an example of how to repeat a string 3 times using Python.

repeated_string = "string" * 3

print(repeated_string)

#Output:
stringstringstring

Creating a List With One Value Repeating in Python

We can also use the Python * operator to repeat list items and create lists with only one value.

Creating a list with only one value can be useful if we want to initialize a list to count or fill later on in our program.

For example, we can create a list of only zeros which we can fill later on.

The easiest way to create a list with only zeros is to use the * operator on a single item array containing 0.

To get a list of 10 zeros for example, we multiply the single item list by 10.

list_of_zeros = [0] * 10

print(list_of_zeros)

#Output:
[0,0,0,0,0,0,0,0,0,0]

You can use this method to create a list which contains any value as shown below in Python.

list_of_a = ["a"] * 10

print(list_of_a)

#Output:
["a","a","a","a","a","a","a","a","a","a"]

Hopefully this article has been useful for you to learn how to repeat a string in Python.

Other Articles You'll Also Like:

  • 1.  pandas cumprod – Find Cumulative Product of Series or DataFrame
  • 2.  How to Remove Vowels from a String in Python
  • 3.  How to Group By Columns and Find Standard Deviation in pandas
  • 4.  How to Check if Variable is Defined in Python
  • 5.  Find Maximum of Three Numbers in Python
  • 6.  Selenium minimize_window() Function to Minimize Window in Python
  • 7.  Initialize Multiple Variables in Python
  • 8.  Changing Python Turtle Size with turtlesize() Function
  • 9.  Draw Circle in Python Using turtle circle() Function
  • 10.  Check if String is Date in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy