Skip to content

text.layout

fit text to a width, convert between naming styles like snake_case and camelCase, and count words

Shapes text for display. Pad, center, truncate or wrap it to a width, switch between naming styles like snake_case and camelCase, and count words.

Tags: formatting

  • pad add fill to the right until the text is width characters, so it lines up on the left
  • pad_left add fill to the left until the text is width characters, so it lines up on the right
  • center center the text in width characters
  • truncate shorten to at most n characters, ending in … if anything was cut
  • wrap break lines between words so none is longer than width characters
  • dedent remove the indentation that every non-blank line has in common
  • title Capitalize The First Letter Of Each Word
  • snake rewrite as snake_case: lowercase words joined by _
  • camel rewrite as camelCase: words joined with no separator, each after the first capitalized
  • kebab rewrite as kebab-case: lowercase words joined by -
  • slug a lowercase version for use in a URL: only letters and digits, with - between words
  • words a list of the words, with punctuation removed except apostrophes
  • wc count lines, words and bytes, like the Unix wc tool
  • word_count how many words there are, counted as words does
pad(s: any, width: int, fill: str = “ “) -> str

add fill to the right until the text is width characters, so it lines up on the left

1
"ab".pad(5) + "|""ab |"
2
"ab".pad(5, ".")"ab..."
Example outputCtrl+Enter to run

See also pad_left, center

pad_left(s: any, width: int, fill: str = “ “) -> str

add fill to the left until the text is width characters, so it lines up on the right

Numbers work too

1
42.pad_left(6, "0")"000042"
Example outputCtrl+Enter to run

See also pad, center

center(s: any, width: int, fill: str = “ “) -> str

center the text in width characters

When the fill can’t split evenly, the extra goes on the right

1
"hi".center(8, "*")"***hi***"
Example outputCtrl+Enter to run

See also pad, pad_left

truncate(s: str, n: int) -> str

shorten to at most n characters, ending in … if anything was cut

1
"a long sentence here".truncate(10)"a long se…"
Example outputCtrl+Enter to run

See also wrap

wrap(s: str, width: int) -> str

break lines between words so none is longer than width characters

1
"the quick brown fox jumps over the lazy dog".wrap(15)
the quick brown fox jumps over the lazy dog
Example outputCtrl+Enter to run

See also truncate, dedent

dedent(s: str) -> str

remove the indentation that every non-blank line has in common

1
" a\n b\n c".dedent
a b c
Example outputCtrl+Enter to run

See also trim, wrap

title(s: str) -> str

Capitalize The First Letter Of Each Word

1
"the lord of the rings".title"The Lord Of The Rings"
Example outputCtrl+Enter to run

See also capitalize, snake

snake(s: str) -> str

rewrite as snake_case: lowercase words joined by _

Words are split at spaces, punctuation and where a lowercase letter meets a capital

1
"parseHTTPRequest".snake"parse_http_request"
2
"Total Price".snake"total_price"
Example outputCtrl+Enter to run

See also camel, kebab

camel(s: str) -> str

rewrite as camelCase: words joined with no separator, each after the first capitalized

1
"user_id_v2".camel"userIdV2"
Example outputCtrl+Enter to run

See also snake, kebab

kebab(s: str) -> str

rewrite as kebab-case: lowercase words joined by -

1
"MyComponentName".kebab"my-component-name"
Example outputCtrl+Enter to run

See also snake, slug

slug(s: str) -> str

a lowercase version for use in a URL: only letters and digits, with - between words

1
"Hello, World! It's 2026".slug"hello-world-its-2026"
Example outputCtrl+Enter to run

See also kebab

words(s: str) -> list

a list of the words, with punctuation removed except apostrophes

1
"Hello, world! Don't panic.".words["Hello", "world", "Don't", "panic"]
Example outputCtrl+Enter to run

See also word_count, split

wc(v: str|bytes, *, lines: bool = false, words: bool = false, bytes: bool = false) -> map|int

count lines, words and bytes, like the Unix wc tool

1
"a b\nc\n".wc{lines: 2, words: 3, bytes: 6}
2
wc("a b\nc\n", words: true)3
Example outputCtrl+Enter to run

As a command:

  • -l, --lines just the line count
  • -w, --words just the word count
  • -c, --bytes just the byte count
wc -l notes.txt
# → wc(read_file("notes.txt"), lines: true)

See also word_count, lines, read_file

word_count(s: str) -> int

how many words there are, counted as words does

1
"Hello, world! Don't panic.".word_count4
Example outputCtrl+Enter to run

See also words

1
# a receipt line
2
"coffee".pad(12, ".") + "$4.50".pad_left(8)"coffee...... $4.50"
3
# a banner
4
"menu".upper.center(20, "=")"========MENU========"
5
# a column name as a variable name
6
"Total Price (USD)".snake"total_price_usd"
7
# a title as a URL slug
8
"Hello, World! It's 2026".slug"hello-world-its-2026"
9
# minutes to read 450 words at 200 a minute
10
(lorem(450).word_count / 200).ceil3
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.