Тип строки в Python имеет очень много удобных методов. Сегодня пост про два таких метода которые чаще всего используются "однобоко". Это методы startswith() и endswith()
Самый обычный сценарий использования — проверка, начинается ли строка с указанной подстроки?
>>> "some_string".startswith("some")
True
И аналогичная ситуация с зеркальным вариантом этой функции, проверка совпадения с конца
>>> "some_string".endswith("some")
False
Так они используются в большинстве случаев что я видел. Но у этих функций есть еще два варианта использования.
🔸Сравнение нескольких подстрок
Для проверки нескольких подстрок в одной строке обычно вызывают эти функции несколько раз. Но на самом деле достаточно передать кортеж со всеми строками один раз. Если будет хоть одно совпадение то функция вернёт True.
>>>"my_image.png".endswith(("jpg", "png", "exr"))
True
🔸Диапазон поиска
Вторым аргументом можно передать индекс символа с которого следует начать сравнение, а третий аргумент это индекс последнего символа.
>>> ".filename.ext".startswith("file", 1)
True
>>> "file_###.ext".endswith('#', 0, -4)
True
Индексы можно указать отрицательными, что означает отсчёт с конца.
#trics#basic
R✨e✨m✨e✨m✨b✨e✨r
Never forget
- Good luck, Have Fun, Don't Die.. 2026 Film putting it right in our faces. Real eyes Realize Real Lies. ..
,•°°•ꨄ•°°•,
✨🌞✨
’•✧•’
( Big Homie )¯\_(ツ)_/¯
All the world is their stage. 🎬
𝐄 𝐍 𝐉 𝐎 𝐘 | 🌟🌟🌿
#Wisdom
It's not what we bought but what we built
It's not what we got but what we shared
It's not our competence but our characters
And it's not our success but our significance
Live a life that matters. Live a life of love.
Unknown
#wisdom
@quietworld🍃
The day the child realizes that all adults are imperfect he becomes an adolescent; the day he forgives them, he becomes an adult; the day he forgives himself he becomes wise.
– Alden N –
#wisdom
@quietworld🍃
We judge people by what they see and hear, but we do not see the qualities that are hidden in their hearts. Avoid suspicion. Let's not say bad things about each other. Let us not gossip or insult.
#wisdom
We can control the words we say, but we can't control what people understand.
We can control what we say to people, but we can't control what other people say to others about us.
That's normal and just a part of life so be it. I'm at peace when I control only what I can control and refuse to take part in things that I cannot control especially those which do not matter at all. ☺️✨🦋
#thoughts#wisdom
@quietworld🍃
To live only for some future goal is shallow. It’s the sides of the mountain that sustain life, not the top.
– Robert M. Prisig
#life#wisdom
@quietworld🍃