String Padding and Repetition Across Languages: Python, JavaScript, SQL, and Bash
Python's str * n, JavaScript's repeat() and padStart(), SQL's RPAD() and LPAD(), and bash printf are all solving the same problem: building strings of specific lengths and patterns. Here's the complete cross-language reference for string repetition and padding with practical formatting examples.
By sadiqbd · June 10, 2026
String padding and repetition are in every programming language — and they solve formatting problems that appear constantly in real code
Generating a line of dashes as a visual separator. Padding a number to a fixed width for a table. Creating a string of dots as a leader in a table of contents. Right-aligning output in a terminal. These are common tasks in console applications, log formatting, code generation, and report generation — and every major language provides built-in functions for them.
Understanding the string repetition and padding functions in your language saves the time of implementing them manually, and using the right function (rather than a loop) produces cleaner, more readable code.
Python: str * n, str.center, str.ljust, str.rjust
String repetition:
"=" * 40
# → "========================================"
"-" * 20
# → "--------------------"
"ab" * 5
# → "ababababab"
Padding:
# Left-justify in a field of width 20, padded with spaces
"Hello".ljust(20)
# → "Hello "
# Right-justify
"42".rjust(8)
# → " 42"
# Zero-pad a number
"42".zfill(8)
# → "00000042"
# Centre with fill character
"Title".center(40, "=")
# → "=================Title=================="
F-string formatting (Python 3.6+):
# Equivalent with f-strings
f"{'Hello':<20}" # left-align in 20-char field
f"{'42':>8}" # right-align in 8-char field
f"{'42':0>8}" # zero-pad to 8 chars
f"{'Title':=^40}" # centre with = fill in 40-char field
f"{3.14159:.2f}" # 2 decimal places: 3.14
f"{1234567:,}" # thousands separator: 1,234,567
JavaScript: String.repeat(), padStart(), padEnd()
Repetition:
"=".repeat(40)
// → "========================================"
"ha".repeat(3)
// → "hahaha"
"-".repeat(0)
// → ""
Padding (ES2017+):
// padStart: pad at the beginning
"42".padStart(8) // " 42"
"42".padStart(8, "0") // "00000042"
"hello".padStart(8, "*") // "***hello"
// padEnd: pad at the end
"hello".padEnd(20) // "hello "
"hello".padEnd(10, ".") // "hello....."
Building a progress bar:
function progressBar(percent, width = 30) {
const filled = Math.round(width * percent / 100);
const empty = width - filled;
return `[${"█".repeat(filled)}${"░".repeat(empty)}] ${percent}%`;
}
progressBar(65);
// → "[████████████████████░░░░░░░░░░] 65%"
SQL: REPEAT(), RPAD(), LPAD()
SQL has string repetition and padding built into most databases:
-- REPEAT(string, count)
SELECT REPEAT('=', 40);
-- → '========================================'
-- LPAD(string, length, pad_string)
SELECT LPAD('42', 8, '0');
-- → '00000042'
SELECT LPAD(invoice_number::text, 8, '0') AS formatted_invoice
FROM invoices;
-- Formats all invoice numbers with leading zeros to 8 digits
-- RPAD(string, length, pad_string)
SELECT RPAD(product_name, 30, '.') || ' £' || price
FROM products;
-- → "Wireless Headphones........... £89.99"
-- → "USB-C Cable.................... £12.99"
bash: printf, yes, and brace expansion
printf for formatted output:
# Right-align numbers in columns
printf "%8d\n" 1 10 100 1000
# 1
# 10
# 100
# 1000
# Zero-pad
printf "%08d\n" 42
# → 00000042
# Create separator line
printf '%0.s=' {1..40}
# → ========================================
Creating repeated strings:
# Using printf
printf '%.s-' {1..20}
# → --------------------
# Using yes command (pipe to head for count)
yes "=" | head -c 40
# → ========================================
# Building a box border
border=$(printf '+%s+' "$(printf '%.s-' {1..38})")
echo "$border"
# → +--------------------------------------+
Go: strings.Repeat(), fmt.Sprintf()
import "strings"
import "fmt"
// Repetition
strings.Repeat("=", 40)
// → "========================================"
strings.Repeat("ab", 5)
// → "ababababab"
// Padding with fmt.Sprintf
fmt.Sprintf("%8d", 42) // " 42" (right-aligned)
fmt.Sprintf("%-8d", 42) // "42 " (left-aligned)
fmt.Sprintf("%08d", 42) // "00000042" (zero-padded)
fmt.Sprintf("%8.2f", 3.14) // " 3.14"
Practical use cases for string repetition and padding
Console table formatting:
print(f"{'Product':<30} {'Price':>10} {'Qty':>6}")
print("-" * 48)
for item in inventory:
print(f"{item.name:<30} {item.price:>10.2f} {item.qty:>6}")
Log line separators:
def log_section(title):
width = 60
separator = "=" * width
print(separator)
print(title.center(width))
print(separator)
Fixed-width file generation:
Many legacy systems (COBOL-era bank data, government file formats) require fixed-width fields. A customer record might specify "last name: 25 characters, right-padded with spaces." Python's ljust() and rjust() make this straightforward.
Generating test data:
# Create a string that hits exactly the maximum field length
max_varchar = "A" * 255 # Test a VARCHAR(255) column
overflow_test = "A" * 256 # Test what happens one character over
How to use the String Repeater on sadiqbd.com
- Enter the string to repeat
- Set the repetition count
- Set separator (optional — string between each repetition)
- Generate — copy the output
- Use for: creating dividers, generating test data, building fill characters for formatted output
Frequently Asked Questions
Is there a maximum string length in different languages?
Python strings have no hard length limit (limited only by available memory). JavaScript strings have a maximum length of 2³⁰ − 2 characters (approximately 1 billion). Java's String maximum is Integer.MAX_VALUE (2³¹ − 1, approximately 2 billion characters). SQL VARCHAR limits are set per column (typically 255 or 65,535 bytes depending on the type). These limits are rarely relevant in practice but matter for stress testing.
Why does "0" * 5 give "00000" in Python but 0 * 5 gives 0?
Python's * operator is overloaded — for strings, it performs repetition; for integers, it performs multiplication. The same symbol, entirely different operations. This is consistent with Python's general approach of making common operations on different types feel natural.
Is the String Repeater free? Yes — completely free, no sign-up required.
String repetition and padding are small tools that appear constantly in formatting-heavy code. Every language provides them as built-in operations precisely because the alternative — writing a loop — is verbose and less readable.
Try the String Repeater free at sadiqbd.com — repeat any string any number of times with optional separator, instantly.