mt.base.str
Some utilities to manipulate strings.
Functions
straighten()
: Straighten a Unicode string to have a fixed length.text_filename()
: Converts a text to a filename or vice versa, replacing special characters with subtexts.
- mt.base.str.straighten(s, length, align_left=True, delimiter=' ')
Straighten a Unicode string to have a fixed length.
- Parameters:
s (str) – string to be trimmed
length (int) – number of characters of the output string
align_left (bool) – whether to align the string to the left (True) or to the right (False)
delimiter (char) – the whitespace character to fill in if the length input string is shorter than the output length
- Returns:
retval – straightened string
- Return type:
str
Examples
>>> from mt.base.str import straighten >>> straighten('Hello world', 7) 'Hello …' >>> straighten('Hi there', 3, align_left=False) '…re' >>> straighten("Oh Carol!", 20, align_left=False) ' Oh Carol!'
- mt.base.str.text_filename(s, forward=True)
Converts a text to a filename or vice versa, replacing special characters with subtexts.
- Parameters:
s (str or bytes) – input text
forward (bool) – whether text to filename (True) or filename to text (False)
- Returns:
filename-friendly output text, same type as input
- Return type:
str or bytes
- Raises:
ValueError – if the text contains a character that cannot be converted