Задать вопрос

Тел: +7 965 3737 888

426

Просмотров

2

Ответов

truncatechars filter

Truncates a string after a certain number of chars.

Question:

Why don't you use the built-in filter slice?

I need the "three points" (...) only when it really truncates.

Вопрос полезен? Да0/Нет0
file_5108.py(580.0б)
None

Ответы (2):

Ответallcaps:04.09.2009
Ответ полезен? Да0/Нет0

The interesting feature with a little problem.

Replace: return value[:length] + '...'

With: return value[:length].rsplit(" ", 1)[0] + ' ...'

>>> truncatechars("Hello World", 6)
'Hello ...'

If the length of the first word is greater or equal to arg, there is an undesirable space! >>> truncatechars("Hello World", 4) 'Hell ...'

Ответsunn:04.09.2009
Ответ полезен? Да0/Нет0

An interesting feature would be if it only cuts whole words - like truncatewords but for chars (length then becomes the maximum length including ...)