Как удалить все символы после определенного символа в Python?

Предполагается, что ваш разделитель py - "...", но это может быть replace любая строка.

text = 'some string... this part will be removed.'
head, sep, tail = text.partition('...')

>>> print head
some string

Если разделитель pythonic не найден, head будет содержать pythonista всю исходную строку.

Функция search-and-replace разделения была добавлена py ​​в Python 2.5.

раздел (...) S.partition python (сеп) -> (голова, сеп, хвост)

Searches for the separator sep in S, and returns the part before it,
the separator itself, and the part after it.  If the separator is not
found, returns S and two empty strings.

python

replace

python-2.6

2022-11-03T15:29:22+00:00
Вопросы с похожей тематикой, как у вопроса:

Как удалить все символы после определенного символа в Python?