Python Forum
How to split string after delimiter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to split string after delimiter
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.

My desired output is :JKl:TOGGLE:HJAK.htm
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]
(Sep-11-2019, 10:58 AM)SriRajesh Wrote: :JKl:TOGGLE:HJAK.htm
Where 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]
or remove l from html
>>> [f':{e}html' for e in s.split('html') if e][0][:-1]
NOTE: my response is using f-strings from python3.6

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 749 Dec-15-2020, 05:21 PM
Last Post: snippsat
  Split string between two different delimiters, with exceptions DreamingInsanity 2 743 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  copy content of text file with three delimiter into excel sheet vinaykumar 0 631 Jul-12-2020, 01:27 PM
Last Post: vinaykumar
  How to print string multiple times separated by delimiter Mekala 1 778 May-23-2020, 09:21 AM
Last Post: Yoriz
  split string enigma619 1 692 May-20-2020, 02:47 PM
Last Post: perfringo
  Split string with multiple delimiters and keep the string in "groups" DreamingInsanity 4 2,044 May-12-2020, 09:31 AM
Last Post: DeaD_EyE
  Split a long string into other strings with no delimiters/characters krewlaz 4 1,087 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  input string split Eric7Giants 3 1,314 Nov-13-2019, 07:19 PM
Last Post: Gribouillis
  [split] capitalize dict keys for display in string newbieAuggie2019 3 1,225 Oct-10-2019, 10:50 AM
Last Post: perfringo
  Merge 2 lines with delimiter Buddhism 9 1,924 May-05-2019, 02:43 AM
Last Post: Buddhism

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020