![]() |
How to split string after delimiter - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to split string after delimiter (/thread-21035.html) |
How to split string after delimiter - SriRajesh - Sep-11-2019 Hi, I have a string as below, I want to split after each "html" and extract the first string after split. I use below code, but I can be able to split after the delimiter. a='JKl:TOGGLE:HJAK.html:Tiger:HJA.html' b=a.split('html')[0]My desired output is :JKl:TOGGLE:HJAK.htm RE: How to split string after delimiter - metulburr - Sep-11-2019 I would just add the htm then after splitting as str.split removes the delimiter. You need to add a portion of it back. There are a number of ways to do that >>> [f'{e}html' for e in s.split('html') if e][0] 'JKl:TOGGLE:HJAK.html' (Sep-11-2019, 10:58 AM)SriRajesh Wrote: :JKl:TOGGLE:HJAK.htmWhere is the first colon coming from as that is not in the original string? If you want to add a colon: >>> [f':{e}html' for e in s.split('html') if e][0] ':JKl:TOGGLE:HJAK.html'or remove l from html >>> [f':{e}html' for e in s.split('html') if e][0][:-1] ':JKl:TOGGLE:HJAK.htm'NOTE: my response is using f-strings from python3.6 |