Is there a shorthand method to repeat a string multiple times?


#1

Question

We can use concatenation to create a string that repeats multiple times by adding itself over and over, but this may become tedious for many repetitions. Is there a shorthand method to repeat a string multiple times?

Answer

In Python, we can do this using the * operator, and essentially multiplying a string by the number of times it should repeat in a newly created string.

Example code

message = "Hi there! "
message_x3 = message * 3

# message_x3 would equal:
# "Hi there! Hi there! Hi there! "