How to Repeat a String N Times (Python, JavaScript, Java, SQL and More)

Repeat a string N times in code — One-liners for 12 languages, and the separator catch

Most languages repeat a string in a single expression. Python and Ruby multiply it: "ab" * 3. JavaScript, Java, Kotlin, Rust and Swift call a method: "ab".repeat(3). PHP, Go and every SQL dialect use a function, though they disagree on its name. All of them return the copies joined with nothing in between — none of them insert a separator.

The one-liner in each language

Each expression below repeats ab three times and returns ababab.

Repeating a string three times, by language
LanguageExpressionNotes
Python"ab" * 3Order does not matter; 3 * "ab" also works
Ruby"ab" * 3Same operator as Python
JavaScript"ab".repeat(3)ES2015 and later
TypeScript"ab".repeat(3)Same method as JavaScript
Java"ab".repeat(3)Java 11 and later only
Kotlin"ab".repeat(3)Standard library, any version
SwiftString(repeating: "ab", count: 3)Initialiser, not a method
Rust"ab".repeat(3)Returns an owned String
Gostrings.Repeat("ab", 3)Requires the strings package
PHPstr_repeat("ab", 3)Underscore, and the count comes second
C#string.Concat(Enumerable.Repeat("ab", 3))No built-in string method
Bashprintf 'ab%.0s' {1..3}The %.0s discards the argument

Java needs version 11

String.repeat() arrived in Java 11. On Java 8, which is still widely deployed, the method does not exist and the code will not compile. The clearest replacement builds the string from a list of copies:

String.join("", Collections.nCopies(3, "ab"))

A StringBuilder in a loop does the same job and is worth using when the count is large, because it avoids allocating an intermediate list.

SQL uses three different names

There is no portable SQL spelling for this. The function exists everywhere and is called something different in each engine.

String repetition by SQL dialect
EngineFunctionExample
SQL ServerREPLICATESELECT REPLICATE('ab', 3)
MySQL / MariaDBREPEATSELECT REPEAT('ab', 3)
PostgreSQLrepeatSELECT repeat('ab', 3)
SQLitereplace trickSELECT replace(hex(zeroblob(3)), '00', 'ab')
OracleRPAD or LPADSELECT RPAD('ab', 6, 'ab') FROM dual

Oracle has no repeat function at all, so the padding functions stand in: pad to a total length of the string length multiplied by the count, using the string itself as the pad. SQLite has no function either, and the zeroblob expression above is the usual workaround.

If the repetition is happening in a spreadsheet rather than a database, the function is REPT in both Excel and Google Sheets — covered in the REPT function guide.

Zero and negative counts do not agree

This is where the languages genuinely differ, and it is a common source of crashes when the count comes from user input or a calculation.

Behaviour with a count of zero and a negative count
LanguageCount of 0Negative count
PythonEmpty stringEmpty string, no error
JavaScriptEmpty stringRangeError thrown
Java 11+Empty stringIllegalArgumentException
GoEmpty stringPanics
MySQLEmpty stringEmpty string

Python is the forgiving one: "ab" * -5 is an empty string rather than an error. JavaScript is the strict one, and because RangeError is thrown rather than returned, an unguarded repeat() on a computed count is a real crash risk. JavaScript also truncates a decimal count, so repeat(2.9) produces two copies.

None of them add a separator

Every expression on this page joins the copies directly. "ab".repeat(3) gives ababab, never ab ab ab. When you need something between the copies there are two options: build the separator into the repeated text, or use a join instead of a repeat.

Repeating with a separator between copies
LanguageComma-separated copies
Python", ".join(["ab"] * 3)
JavaScriptArray(3).fill("ab").join(", ")
Java 11+String.join(", ", Collections.nCopies(3, "ab"))
Gostrings.Join(make([]string, 3), ", ") after filling
PHPimplode(", ", array_fill(0, 3, "ab"))

The join approach is the one to reach for, because it puts the separator only between copies and not after the last one. Baking the separator into the text — "ab, " * 3 — leaves a trailing comma you then have to strip.

When not to write code at all

Repetition in code is the right call inside a program. It is the wrong call when you need the output once: a block of placeholder text, a string of an exact length to paste into a form, or a list of copies to hand to someone else. Writing, running and copying out of a script is slower than typing the text and the count into a field.

Generating a string of an exact length for testing is its own task, with boundary values worth knowing — see testing character limits with repeated text.

Need the repeated text once, not in a program? Enter the text and the count, pick a separator, and copy the result — no script, no environment.

Open the text repeater

Frequently asked questions

How do you repeat a string n times in Python?

Multiply the string by the number: "ab" * 3 returns ababab. The operator works in either order, so 3 * "ab" is identical. A count of zero or any negative number returns an empty string rather than raising an error.

How do you repeat a string in JavaScript?

Use the repeat method: "ab".repeat(3) returns ababab. It has been available since ES2015. A negative count throws a RangeError, and a decimal count is truncated, so repeat(2.9) produces two copies.

Does String.repeat work in every version of Java?

No. String.repeat was added in Java 11, so it does not compile on Java 8 or 9. The usual replacement is String.join("", Collections.nCopies(3, "ab")), or appending to a StringBuilder in a loop when the count is large.

How do you repeat a string in SQL?

It depends on the engine. SQL Server uses REPLICATE('ab', 3), MySQL and MariaDB use REPEAT('ab', 3), and PostgreSQL uses repeat('ab', 3). Oracle has no such function and uses RPAD instead, and SQLite needs a replace and zeroblob workaround.

How do you repeat a string with a separator between copies?

Use a join rather than a repeat, because a join places the separator only between copies and not after the final one. In Python that is ", ".join(["ab"] * 3), and in JavaScript it is Array(3).fill("ab").join(", ").

What happens if the repeat count is zero or negative?

A count of zero returns an empty string in every language on this page. A negative count differs: Python returns an empty string, MySQL returns an empty string, JavaScript throws a RangeError, Java throws an IllegalArgumentException, and Go panics.

← All articles