Python Forum
Get the string after a specific char
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get the string after a specific char
#1
Hi,

I am a newbie in Python and would like help on achieving getting the string after a specific character. For example below, how do i retrieve the character after the 2nd occurence of * which in this case OH.


N4*COLUMBUS*OH*43230*US

Thank you in advance for your help.
Reply
#2
Split at *, take the third element of the resulting list
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
This can also be done with the re module
>>> import re
>>> data = "N4*COLUMBUS*OH*43230*US"
>>> match = re.match(r'(?:[^*]*\*){2}([^*]*)', data)
>>> match.group(1)
'OH'
>>> 
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 2,664 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 3,276 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  How to extract specific key value pair from string? aditi06 0 3,255 Apr-15-2021, 06:26 PM
Last Post: aditi06
  How to replace on char with another in a string? korenron 3 3,100 Dec-03-2020, 07:37 AM
Last Post: korenron
  How to remove char from string?? ridgerunnersjw 2 3,303 Sep-30-2020, 03:49 PM
Last Post: ridgerunnersjw
  Printing string at specific position on terminal - not showing __Mathieu__ 1 3,411 Sep-07-2020, 10:32 AM
Last Post: Larz60+
  Highlight and remove specific string of text itsalmade 5 4,709 Dec-11-2019, 11:58 PM
Last Post: micseydel
  Sum char word dictionary RavCOder 3 3,516 Nov-08-2019, 01:32 PM
Last Post: perfringo
  Delete specific lines contain specific words mannyi 2 5,060 Nov-04-2019, 04:50 PM
Last Post: mannyi
  The use of escape char \ hishamzero1 2 3,067 Aug-12-2019, 10:20 PM
Last Post: hishamzero1

Forum Jump:

User Panel Messages

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