C
C
Crowcloud2020-07-26 21:33:04
Python
Crowcloud, 2020-07-26 21:33:04

How to use .split while keeping delimiter characters in output?

There is a challenge in the book "Your Own Programmer" (Corey Althoff).
Primitive, but with it there was a problem.

Call the method that turns the string 'Where is this? Who is it? When is that?' to the list ['Where is this?', 'Who is this?', 'When is this?']


This is clearly about .split
But if a = 'Where is this? Who is it? When is that?' , then:

print(a.split(' '))
#output['Where', 'is this?', 'Who', 'is this?', 'When', 'is this?']

print(a.split(' ?'))
#output['Where is this', ' Who is this', ' When is this', '']

print(a.split('? '))
#output['Where is this', 'Who is this', ' When is it?'

] "Where is this" != "Where is this?" and "Who is this" != "Who is this?" , the condition of the task is not met.

What am I doing wrong?
How is it done correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kocherman, 2020-07-26
@Crowcloud

import re
res = list(filter(None, re.split('([^?]+\?)','Где это? Кто это? Когда это?')))
print(res)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question