Skip to content

pad

Pads a string until it reaches the given length. One function covers all three directions through the position option, so it replaces Lodash's pad, padStart and padEnd.

Padding is added on both sides by default, matching Lodash's pad. When the two sides cannot be equal the extra character goes to the end, so pad('abc', 8) returns ' abc '.

A char longer than one character is repeated and cut off where it no longer fits: pad('abc', 8, { char: '_-' }) returns '_-abc_-_'.

The string is returned untouched when it is already at least length long, and when char is empty. Length is counted in code points, so a character outside the Basic Multilingual Plane counts as one in every language.

Parameters

NameTypeRequiredDefault
textstringStringstr
The string to pad. A missing value is treated as an empty string and is padded to the full length.
lengthnumberint
The length to pad up to, counted in code points.
optionsnamedkeywordPadOptions
Padding options. See the table below.

namedThese parameters are passed as named parameters.

keywordThese parameters are passed as keyword arguments, or as a single dict in their place.

PadOptions
NameTypeRequiredDefault
position'start' | 'end' | 'both''both'
Which side to pad. both splits the padding, giving the extra character to the end.
charstringStringstr' '
The characters to pad with. A value longer than one character is repeated and truncated. An empty value returns the string untouched.

Returns

string

String

str

Examples

javascript
pad('abc', 8); // Returns '  abc   '
pad('abc', 8, { char: '_-' }); // Returns '_-abc_-_'
pad('abc', 8, { position: 'start' }); // Returns '     abc'
pad('abc', 8, { position: 'end' }); // Returns 'abc     '
pad('5', 3, { position: 'start', char: '0' }); // Returns '005'
pad('abcdefgh', 4); // Returns 'abcdefgh'
dart
pad('abc', 8); // Returns '  abc   '
pad('abc', 8, char: '_-'); // Returns '_-abc_-_'
pad('abc', 8, position: 'start'); // Returns '     abc'
pad('abc', 8, position: 'end'); // Returns 'abc     '
pad('5', 3, position: 'start', char: '0'); // Returns '005'
pad('abcdefgh', 4); // Returns 'abcdefgh'
python
pad('abc', 8)  # Returns '  abc   '
pad('abc', 8, {'char': '_-'})  # Returns '_-abc_-_'
pad('abc', 8, {'position': 'start'})  # Returns '     abc'
pad('abc', 8, {'position': 'end'})  # Returns 'abc     '
pad('5', 3, position='start', char='0')  # Returns '005'
pad('abcdefgh', 4)  # Returns 'abcdefgh'

Released under the MIT License