-3

I got an example string:

"Santonio-Spurs: a great basketball team"

What is the easiest way to split it by space and 'symbol :'

Should I split it by space first and then split it by symbol : ?

The output I expected is:

['Santonio-Spurs', 'a', 'great', 'basketball', 'team']
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Zen
  • 4,381
  • 5
  • 29
  • 56

1 Answers1

1

Python's standard regular expression module is your friend:

>>> import re
>>> re.split('[:\s]+', "Santonio-Spurs: a great basketball team")
['Santonio-Spurs', 'a', 'great', 'basketball', 'team']

The [:\s] part means "the ':' character or a space character", and the + means "1 or multiple times" (which takes care of a sequence of multiple separation characters like ": " in your input string).

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
  • 1
    Can you point to the exact duplicate?? I fail to see it. – Eric O. Lebigot May 15 '14 at 04:45
  • If I am not mistaken, downvoting answers to a possible duplicate question is not recommended by the StackOverflow guidelines. One reason is that (1) you cannot downvote your own answer and (2) in order to be consistent you would have to keep an eye on all future answers. Marking the original question as a possible duplicate is good, though. – Eric O. Lebigot May 15 '14 at 04:50
  • Why the downvotes?? Steering away Python programmers from this canonical and efficient solution is a very, very bad idea, in my opinion. – Eric O. Lebigot May 15 '14 at 04:52
  • I've removed my downvote, although people who downvoted my answer don't care about SO guidelines. Apparently *nobody* cares about them. – vaultah May 15 '14 at 05:31