Skip to content

Commit bb34347

Browse files
Renamed and improved python truncate snippet
Renamed from `truncate-string.py` to `truncate.py` Replaced "..." with ellipses("…") to only use one character Implemented toggle for the `suffix` "…" on string with default of `True`
1 parent 3f3137a commit bb34347

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

snippets/python/string-manipulation/truncate-string.md

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Truncate String
3+
description: Truncates a string to a specified length and adds a toggleable ellipsis.
4+
author: minerminermain
5+
tags: string,truncate
6+
---
7+
8+
```py
9+
def truncate(s:str, length:int, suffix:bool = True) -> str :
10+
return (s[:length] + ("" if suffix else "")) if len(s) > length else s
11+
12+
# Usage:
13+
truncate('This is a long string', 10) # Returns: 'This is a …'
14+
truncate('This is a long string', 10, False) # Returns: 'This is a '
15+
```

0 commit comments

Comments
 (0)