Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

68 lines
1.5 KiB
Markdown

---
id: 69738771fb5a7b8b24cca2a4
title: "Challenge 178: Truncate the Text"
challengeType: 29
dashedName: challenge-178
---
# --description--
Given a string, return it as-is if it's 20 characters or shorter. If it's longer than 20 characters, truncate it to the first 17 characters and append `"..."` to the end of it (so it's 20 characters total) and return the result.
# --hints--
`truncate_text("Hello, world!")` should return `"Hello, world!"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(truncate_text("Hello, world!"), "Hello, world!")`)
}})
```
`truncate_text("This string should get truncated.")` should return `"This string shoul..."`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(truncate_text("This string should get truncated."), "This string shoul...")`)
}})
```
`truncate_text("Exactly twenty chars")` should return `"Exactly twenty chars"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(truncate_text("Exactly twenty chars"), "Exactly twenty chars")`)
}})
```
`truncate_text(".....................")` should return `"...................."`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(truncate_text("....................."), "....................")`)
}})
```
# --seed--
## --seed-contents--
```py
def truncate_text(text):
return text
```
# --solutions--
```py
def truncate_text(text):
if len(text) <= 20:
return text
return text[:17] + "..."
```