Тип строки в 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
2019 Chinese University of Hong Kong (CUHK) Siege Timeline:
- Nov 11-12: Police initiate action by entering CUHK campus, sparking clashes.
- Nov 12: Police deploy tear gas, rubber bullets, and bean bag rounds; clashes last for hours, with dozens injured.
- Nov 12 (Evening): University president Rocky Tuan mediates; police agree to withdraw, but tensions persist.
- Nov 13: Aftermath continues as campus calms, becoming a symbol of the protests.
- Police Brutality Details: According to Amnesty International and Human Rights Watch reports, police used excessive force, including close-range rubber bullet shots causing eye injuries, beatings of detainees, and indiscriminate tear gas affecting students and non-protesters.
#HongKongProtests#2019HongKong
Timeline of Chow Tsz-lok's Death:
- Nov 3, 2019 (Evening): Protests in Tseung Kwan O escalate; police deploy tear gas.
- Nov 4, 2019 (Around 1 AM): Chow falls from 3rd floor of Sheung Tak Estate car park, suffers severe injuries; reports claim police were present at the parking lot at the time.
- Car park fence is 1.3m tall, making accidental fall unlikely for an adult, raising suspicions.
- Chow had severe head injuries but no other broken bones; critics claim this proves he was unconscious before the fall.
- Nov 4, 2019: Ambulance delayed due to roadblocks, with reports claiming police cars blocked the way; Chow admitted to hospital in coma.
- Nov 8, 2019 (Morning): Chow pronounced dead at age 22 from cardiac arrest.
- Jan 2021: Coroner's inquest did not rule accidental death, but exact cause of fall undetermined; critics allege the judge misled the jury multiple times that it was an accident.
- A tragic symbol of Hong Kong's 2019 protests—remembering the call for justice.
#ChowTszLok#HongKongProtests
Legal expert Johannes Chan Man-mun explains the permanent nature of the "interim" injunction in Hong Kong's protest case, with no avenue for appeal to the Court of Final Appeal. Chan criticizes the court's decision to deny intervention, highlighting the challenges in challenging the ban. He emphasizes the undermining of human rights and suggests legislative action instead of relying on court injunctions. Human rights concerns persist in Hong Kong. #HongKongProtests#LegalRuling#HumanRights