hbutils.string.trunc¶
- Overview:
Useful utilities for truncate (shorten) your string.
truncate¶
-
hbutils.string.trunc.
truncate
(text: str, width: int = 70, tail_length: int = 0, show_length: bool = False)[source]¶ - Overview:
Truncate string into short form.
- Arguments:
text (
str
): Original text to be truncated.width (
int
): Final width of the new string, default is70
.tail_length (
int
): Tail’s length of the new string, default is0
which means no tail.show_length (
bool
): Show length in middle part or not, default isFalse
which means do not show this.
- Returns:
shortened (
str
) Short-formed string.
- 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 '