Text Repeater: How to Repeat Text N Times
Repeating a string is one of the most common text-manipulation tasks — from generating test data and dummy content to creating separators, padding, and stress-testing inputs. This guide explains how to repeat text online (no code needed) and how to do it programmatically in JavaScript, Python, Go, and PHP.
Why Repeat Text?
| Use case | Example |
|---|---|
| Test data generation | Paste 500 copies of "Lorem ipsum" into a load tester |
| Separator lines | = repeated 80 times gives a clean horizontal rule |
| String padding | Repeat "0" to left-pad a number |
| Stress testing | Repeat an input to trigger buffer-overflow edge cases |
| Lorem ipsum substitute | Repeat a sentence until you fill a design mock-up |
| CSV/TSV generation | Repeat a row template and fill it with varied values |
| Poetry & word art | Repeat a phrase to build rhythm or visual patterns |
Repeat Text Online (No Code)
The fastest way is to use a free online text repeater:
- Paste or type your text into the input field.
- Set the number of repetitions (1–10,000).
- Choose a separator: new line, space, comma, or a custom string.
- Copy the output.
Everything runs in your browser — nothing is uploaded and there are no sign-ups or limits.
Choosing a Separator
The separator appears between each copy of your text. Common options:
| Separator | Symbol | Typical use |
|---|---|---|
| New line | \n |
One copy per line — great for lists |
| Space | |
Words repeated in a sentence |
| Comma | , |
CSV-ready output |
| Comma + space | , |
Human-readable lists |
| None (empty) | `` | Characters concatenated: aaaa |
| Custom | any string | Pipe-delimited, semicolons, ---, etc. |
Tip: if you want hello hello hello, choose the space separator. If you need one hello per line for a bulk import, choose new line.
How to Repeat a String in Code
JavaScript
JavaScript has a built-in String.prototype.repeat() method, available in all modern browsers and Node.js:
// Basic repeat
"ha".repeat(3); // "hahaha"
"hello".repeat(5); // "hellohellohellohellohello"
// Repeat with separator — join an array
Array(5).fill("hello").join("\n");
// hello
// hello
// hello
// hello
// hello
// Repeat with comma separator
Array(4).fill("item").join(", ");
// "item, item, item, item"
// Utility function
function repeatText(text, times, separator = "") {
if (times < 0) throw new RangeError("times must be >= 0");
return Array(times).fill(text).join(separator);
}
repeatText("abc", 3, "-"); // "abc-abc-abc"
repeatText("*", 10); // "**********"
Edge cases:
"x".repeat(0)returns""(empty string) — not an error."x".repeat(-1)throwsRangeError: Invalid count value."x".repeat(Infinity)also throwsRangeError.
Python
Python overloads the * operator for string repetition:
# Basic repeat
"ha" * 3 # 'hahaha'
"hello" * 5 # 'hellohellohellohellohello'
# Repeat with separator
"\n".join(["hello"] * 5)
# hello
# hello
# hello
# hello
# hello
", ".join(["item"] * 4)
# 'item, item, item, item'
# Utility function
def repeat_text(text: str, times: int, separator: str = "") -> str:
if times < 0:
raise ValueError("times must be >= 0")
return separator.join([text] * times)
repeat_text("abc", 3, "-") # 'abc-abc-abc'
repeat_text("*", 10) # '**********'
Python also supports:
# Padding with ljust/rjust/center (uses repeat internally)
"7".rjust(5, "0") # '00007'
"hi".center(10, "-") # '----hi----'
Go
Go uses strings.Repeat() from the standard library:
package main
import (
"fmt"
"strings"
)
func main() {
// Basic repeat
fmt.Println(strings.Repeat("ha", 3)) // hahaha
fmt.Println(strings.Repeat("hello", 5)) // hellohellohellohellohello
// Repeat with separator
copies := make([]string, 5)
for i := range copies {
copies[i] = "hello"
}
fmt.Println(strings.Join(copies, "\n"))
// hello
// hello
// hello
// hello
// hello
// Repeat with comma
fmt.Println(strings.Join(copies[:4], ", "))
// hello, hello, hello, hello
}
// Utility function
func repeatText(text string, times int, separator string) string {
if times <= 0 {
return ""
}
copies := make([]string, times)
for i := range copies {
copies[i] = text
}
return strings.Join(copies, separator)
}
Note: strings.Repeat(s, 0) returns "". Negative counts panic with strings: negative Repeat count.
PHP
PHP provides str_repeat():
<?php
// Basic repeat
echo str_repeat("ha", 3); // hahaha
echo str_repeat("hello", 5); // hellohellohellohellohello
// Repeat with separator
echo implode("\n", array_fill(0, 5, "hello"));
// hello
// hello
// hello
// hello
// hello
echo implode(", ", array_fill(0, 4, "item"));
// item, item, item, item
// Utility function
function repeatText(string $text, int $times, string $separator = ""): string {
if ($times < 0) {
throw new \InvalidArgumentException("times must be >= 0");
}
if ($times === 0) return "";
return implode($separator, array_fill(0, $times, $text));
}
echo repeatText("abc", 3, "-"); // abc-abc-abc
echo repeatText("*", 10); // **********
Language Quick-Reference Table
| Language | Built-in | With separator |
|---|---|---|
| JavaScript | "x".repeat(n) |
Array(n).fill("x").join(sep) |
| Python | "x" * n |
sep.join(["x"] * n) |
| Go | strings.Repeat("x", n) |
strings.Join(slice, sep) |
| PHP | str_repeat("x", n) |
implode(sep, array_fill(0, n, "x")) |
| Bash | printf 'x%.0s' {1..n} |
printf 'x%s' ... (custom loop) |
| Ruby | "x" * n |
Array.new(n, "x").join(sep) |
| Rust | "x".repeat(n) |
vec!["x"; n].join(sep) |
| C# | new string('x', n) (char) / string.Concat(Enumerable.Repeat("x", n)) |
string.Join(sep, ...) |
Practical Examples
Generate a horizontal rule
// 80-character separator line
"=".repeat(80);
// ================================================================================
Indent code
indent_level = 3
indent = " " * indent_level # 12 spaces
print(f"{indent}return x")
# " return x"
Build a test CSV
header := "id,name,value\n"
row := "1,test,100\n"
csv := header + strings.Repeat(row, 1000)
// 1001-line CSV for load testing
Left-pad a number
$padded = str_repeat("0", 5 - strlen("42")) . "42"; // "00042"
// Or use str_pad:
$padded = str_pad("42", 5, "0", STR_PAD_LEFT); // "00042"
Generate a password pattern placeholder
"*".repeat(password.length); // hide length-aware placeholders
Performance Considerations
For most use cases (< 100,000 characters), any approach is fast enough. For very large repetitions:
| Approach | Performance | Notes |
|---|---|---|
| Built-in repeat | Fastest | Allocated in a single pass |
| Array fill + join | Fast | One extra allocation for the array |
| String concatenation in a loop | Slow | O(n²) due to string immutability |
| StringBuilder / Buffer | Fast for loops | Use when building with mutation |
Avoid this pattern in performance-sensitive code:
// BAD — O(n²) in JavaScript
let result = "";
for (let i = 0; i < 10000; i++) {
result += "hello"; // creates a new string each iteration
}
Prefer:
// GOOD — single allocation
const result = "hello".repeat(10000);
Common Mistakes
| Mistake | Problem | Fix |
|---|---|---|
| Off-by-one | Repeating 4 times when you need 5 copies | Count copies, not gaps |
| Forgetting the separator | Output is one long unbroken string | Set separator = "\n" or similar |
| Using string concatenation in a loop | O(n²) performance | Use built-in repeat or join |
| Negative count | Runtime error / panic | Validate times >= 0 before calling |
| Repeat on undefined | undefined.repeat(3) throws |
Ensure input is a string |
FAQ
Can I repeat multiple lines?
Yes. Your "text" can contain newlines — paste an entire paragraph and repeat the whole block. The separator is added between each copy.
Is there a limit on how many times I can repeat?
In code, the limit is your available memory. A string of 1 MB repeated 1,000 times produces ~1 GB — make sure you have headroom. Online tools typically cap at 10,000 repetitions to keep the browser responsive.
How do I repeat a character without a separator?
Use an empty string as the separator, or use the built-in method directly: "*".repeat(20) → "********************".
What's the difference between str_repeat and str_pad in PHP?str_repeat repeats the string an exact number of times. str_pad pads a string to a target length, padding with a fill character. Use str_pad for fixed-width output and str_repeat when you need an exact count.
How do I repeat text and then remove the last separator?
With join-based approaches, the separator only appears between copies, never at the end. With the concatenation approach, strip the last separator character: result.slice(0, -separator.length).
Can I repeat Unicode, emoji, or multi-byte characters?
Yes. Modern built-ins (str.repeat, "x" * n, strings.Repeat) operate on code points, not bytes, so "😀".repeat(3) → "😀😀😀" is safe.