Skip to content

Text Manipulation

Text manipulation helpers.

Manipulation Module

Text manipulation utilities.

This submodule provides utilities for manipulating text including truncation, padding, and wrapping.

Examples

from rite.text.manipulation import text_truncate, text_pad_left text_truncate("Hello World", 8) 'Hello...' text_pad_left("5", 3, "0") '005'

Modules

text_pad_left

Text Pad Left

Pad text on the left.

Examples

from rite.text.manipulation import text_pad_left text_pad_left("5", 3, "0") '005'

Functions

text_pad_left(text: str, width: int, fill_char: str = ' ') -> str

Pad text on the left to width.

Parameters:

Name Type Description Default
text str

Text to pad.

required
width int

Target width.

required
fill_char str

Character to pad with.

' '

Returns:

Type Description
str

Left-padded text.

Examples:

>>> text_pad_left("5", 3, "0")
'005'
>>> text_pad_left("Hi", 5)
'   Hi'
Notes

Uses str.rjust() method.

text_pad_right

Text Pad Right

Pad text on the right.

Examples

from rite.text.manipulation import text_pad_right text_pad_right("Hi", 5) 'Hi '

Functions

text_pad_right(text: str, width: int, fill_char: str = ' ') -> str

Pad text on the right to width.

Parameters:

Name Type Description Default
text str

Text to pad.

required
width int

Target width.

required
fill_char str

Character to pad with.

' '

Returns:

Type Description
str

Right-padded text.

Examples:

>>> text_pad_right("Hi", 5)
'Hi   '
>>> text_pad_right("5", 3, "0")
'500'
Notes

Uses str.ljust() method.

text_truncate

Text Truncate

Truncate text to maximum length.

Examples

from rite.text.manipulation import text_truncate text_truncate("Hello World", 5) 'Hello...'

Functions

text_truncate(text: str, max_length: int, suffix: str = '...') -> str

Truncate text to maximum length.

Parameters:

Name Type Description Default
text str

Text to truncate.

required
max_length int

Maximum length (including suffix).

required
suffix str

Suffix to add when truncated.

'...'

Returns:

Type Description
str

Truncated text with suffix if needed.

Examples:

>>> text_truncate("Hello World", 5)
'Hello...'
>>> text_truncate("Hi", 10)
'Hi'
Notes

Suffix length is included in max_length.

text_wrap

Text Wrap

Wrap text to specified width.

Examples

from rite.text.manipulation import text_wrap text_wrap("Hello World", 5) ['Hello', 'World']

Functions

text_wrap(text: str, width: int = 70) -> list[str]

Wrap text to specified width.

Parameters:

Name Type Description Default
text str

Text to wrap.

required
width int

Maximum line width.

70

Returns:

Type Description
list[str]

List of wrapped lines.

Examples:

>>> text_wrap("Hello World", 5)
['Hello', 'World']
>>> text_wrap("A long sentence", 10)
['A long', 'sentence']
Notes

Uses textwrap.wrap() from stdlib.

options: show_root_heading: true show_source: false heading_level: 2