Python Forum
doing string split with 2 or more split characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
doing string split with 2 or more split characters
#3
Can't see a one-liner to do this.

# add a unwanted character at the end of the string or you won't get the last wanted characters
mystring = 'ab|cd!ef|gh!ij|kl!mn|'
# define what you want to keep
wanted = 'abcdefghijklmnopqrstuvwxyz'
count = 0
for i in range(len(mystring)):    
    if not mystring[i] in wanted:
        seq = mystring[count:i]
        print(seq)
        count = i+1
Reply


Messages In This Thread
RE: doing string split with 2 or more split characters - by Pedroski55 - Aug-04-2023, 06:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class and methods ebn852_pan 15 800 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  Class test : good way to split methods into several files paul18fr 4 617 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  [split] Pipenv mohammadasadi4 0 346 Jan-15-2024, 10:35 AM
Last Post: mohammadasadi4
  [split] Why is there an output of None akbarza 1 546 Nov-27-2023, 02:53 PM
Last Post: deanhystad
  [split] Class takes no arguments bily071 2 714 Oct-23-2023, 03:59 PM
Last Post: deanhystad
  [split] Issue installing selenium Akshat_Vashisht 1 619 Oct-18-2023, 02:08 PM
Last Post: Larz60+
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,294 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  How to "tee" (=split) output to screen and into file? pstein 6 1,525 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  [split] How to resolve version conflicts in Python? atonalwilson 1 1,060 May-04-2023, 09:02 AM
Last Post: buran
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,661 Apr-12-2023, 10:39 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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