File tree Expand file tree Collapse file tree 1 file changed +3
-3
lines changed
snippets/python/string-manipulation Expand file tree Collapse file tree 1 file changed +3
-3
lines changed Original file line number Diff line number Diff line change 11---
22title : Truncate String
3- description : Truncates a string to a specified length and adds a toggleable ellipsis .
3+ description : Truncates a string to a specified length and a toggleable truncation notation .
44author : axorax
55contributors : MinerMinerMods
66tags : string,truncate
77---
88
99``` py
1010def truncate (s :str , length :int , suffix :bool = True ) -> str :
11- return (s[:length] + (" … " if suffix else " " )) if len (s) > length else s
11+ return (s[:length] + (" ... " if suffix else " " )) if len (s) > length else s
1212
1313# Usage:
14- truncate(' This is a long string' , 10 ) # Returns: 'This is a … '
14+ truncate(' This is a long string' , 10 ) # Returns: 'This is a ... '
1515truncate(' This is a long string' , 10 , False ) # Returns: 'This is a '
1616```
You can’t perform that action at this time.
0 commit comments