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
#1
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
Reply
#2
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.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]
':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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string between two different delimiters, with exceptions DreamingInsanity 2 271 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  copy content of text file with three delimiter into excel sheet vinaykumar 0 246 Jul-12-2020, 01:27 PM
Last Post: vinaykumar
  How to print string multiple times separated by delimiter Mekala 1 358 May-23-2020, 09:21 AM
Last Post: Yoriz
  split string enigma619 1 272 May-20-2020, 02:47 PM
Last Post: perfringo
  Split string with multiple delimiters and keep the string in "groups" DreamingInsanity 4 522 May-12-2020, 09:31 AM
Last Post: DeaD_EyE
  Split a long string into other strings with no delimiters/characters krewlaz 4 548 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  input string split Eric7Giants 3 751 Nov-13-2019, 07:19 PM
Last Post: Gribouillis
  [split] capitalize dict keys for display in string newbieAuggie2019 3 694 Oct-10-2019, 10:50 AM
Last Post: perfringo
  Merge 2 lines with delimiter Buddhism 9 1,131 May-05-2019, 02:43 AM
Last Post: Buddhism
  How to split at specified delimiter SriRajesh 5 955 Dec-27-2018, 03:43 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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