hbutils.string.trunc
- Overview:
Useful utilities for truncate (shorten) your string.
This module provides functionality to truncate long strings into shorter forms with customizable options for width, tail display, and length indication.
truncate
- hbutils.string.trunc.truncate(text: str, width: int = 70, tail_length: int = 0, show_length: bool = False) str[source]
Truncate string into short form.
This function shortens a given text to a specified width, optionally showing the tail portion of the original text and/or the total character count.
- Parameters:
text (str) – Original text to be truncated.
width (int) – Final width of the new string, default is
70.tail_length (int) – Tail’s length of the new string, default is
0which means no tail.show_length (bool) – Show length in middle part or not, default is
Falsewhich means do not show this.
- Returns:
Short-formed string.
- Return type:
str
- Examples::
>>> from hbutils.string import truncate >>> truncate('abc ' * 30, width=30) 'abc abc abc abc abc abc ... ' >>> truncate('abc ' * 30, width=40, tail_length=10) 'abc abc abc abc abc abc ... c abc abc ' >>> truncate('abc ' * 30, width=40, tail_length=10, show_length=True) 'abc abc abc ..(120 chars).. c abc abc '