Repeat Strings in Code: JavaScript, Python and More

Repeat strings with or without a separator

Use these examples when adding string repetition to your own code. Built-in repetition usually joins copies directly. Use a join operation when you want spaces or another separator. Start with a small count; large strings use memory and language limits differ.

The Java examples use Java 11 or later. Keep the import at file scope and put the statements inside a main method or run them in JShell. The C# example uses top-level statements; older projects can place them inside Main.

How to repeat text in code

If you need string repetition inside your own application, the examples below show the standard approach in common languages.

JavaScript – the .repeat() method

JavaScript’s built-in string method is the most concise approach.

// Basic repetition
const text = "Hello";
const repeated = text.repeat(5);
console.log(repeated);
// Output: "HelloHelloHelloHelloHello"

// With a separator
function repeatWithSeparator(text, count, separator) {
  return Array(count).fill(text).join(separator);
}
console.log(repeatWithSeparator("Hello", 3, " "));
// Output: "Hello Hello Hello"

Python – the * operator

Python uses the multiplication operator to repeat strings.

# Basic repetition
text = "Hello"
repeated = text * 5
print(repeated)
# Output: "HelloHelloHelloHelloHello"

# With a separator
separator = " "
repeated_with_sep = separator.join([text] * 5)
print(repeated_with_sep)
# Output: "Hello Hello Hello Hello Hello"

# With auto-increment
for i in range(1, 6):
    print(f"{i}. {text}")
# Output: "1. Hello", "2. Hello", "3. Hello", "4. Hello", "5. Hello"

Java – String.repeat() (Java 11+)

Java 11 added a native repeat() method for strings.

// Basic repetition
import java.util.Collections;

String text = "Hello";
String repeated = text.repeat(5);
System.out.println(repeated);
// Output: "HelloHelloHelloHelloHello"

// With a separator (import java.util.Collections)
String separator = " ";
String repeatedWithSep = String.join(separator, Collections.nCopies(5, text));
System.out.println(repeatedWithSep);
// Output: "Hello Hello Hello Hello Hello"

C# – Enumerable.Repeat()

using System;
using System.Linq;
string text = "Hello";
string repeatedText = string.Concat(Enumerable.Repeat(text, 5));
Console.WriteLine(repeatedText);
// Output: "HelloHelloHelloHelloHello"

// With separator
string separator = " ";
string repeatedWithSep = string.Join(separator, Enumerable.Repeat(text, 5));
Console.WriteLine(repeatedWithSep);
// Output: "Hello Hello Hello Hello Hello"

PHP – str_repeat()

$text = "Hello";
$repeated = str_repeat($text, 5);
echo $repeated;
// Output: "HelloHelloHelloHelloHello"

$separator = " ";
$repeatedWithSep = implode($separator, array_fill(0, 5, $text));
echo $repeatedWithSep;
// Output: "Hello Hello Hello Hello Hello"

Ruby – the * operator

text = "Hello"
repeated = text * 5
puts repeated
# Output: "HelloHelloHelloHelloHello"

separator = " "
repeated_with_sep = ([text] * 5).join(separator)
puts repeated_with_sep
# Output: "Hello Hello Hello Hello Hello"

Quick reference: string repetition by language

LanguageMethodExample
JavaScript.repeat()"Hello".repeat(5)
Python* operator"Hello" * 5
Java.repeat() (Java 11+)"Hello".repeat(5)
C#Enumerable.Repeat()string.Concat(Enumerable.Repeat("Hello", 5))
PHPstr_repeat()str_repeat("Hello", 5)
Ruby* operator"Hello" * 5
Gostrings.Repeat()strings.Repeat("Hello", 5)
Rust.repeat()"Hello".repeat(5)
SwiftString(repeating:count:)String(repeating: "Hello", count: 5)