-2

For example I have followed string:

names = "JohnDoeEmmyGooseRichardNickson"

How can I split that string based on the title of the words? So every time a capital letter occurs, the string will be split up.

Is there a way to do this with the split() method? (no regex)

That I will get:

namesL = ["John","Doe","Emmy","Goose","Richard","Nickson"]
JAdel
  • 1,309
  • 1
  • 7
  • 24

2 Answers2

1

You can do it with regex

>>> import re
>>> s = "TheLongAndWindingRoad ABC A123B45"
>>> re.sub( r"([A-Z])", r" \1", s).split()
# output
['The', 'Long', 'And', 'Winding', 'Road', 'A', 'B', 'C', 'A123', 'B45']
Jonatrios
  • 424
  • 2
  • 5
0

Can't do it with the split method, but doable with re:

import re
namesL = re.split("(?=[A-Z])", names)[1:]

Keep in mind the first entry will be an empty string (as the first word is also capitalized) so we're removing it.

Bharel
  • 23,672
  • 5
  • 40
  • 80