M
M
Mike2020-06-03 18:01:00
Python
Mike, 2020-06-03 18:01:00

How to change a line and dot it?

There is a line that contains something like this `var = 'When parents goWhat do they listen toWhy How' `
As an output, I need to get `When parents go. What do they listen to. Why How` . But my code below puts a period before the word 'How' `When parents go. What do they listen to. why. How`
_

var = 'When parents goWhat do they listen toWhy How'
words = ['When', 'Why', 'What', 'How']
for word in words:
    if not var.startswith(word):
        var = var.replace(word, f'. {word}')
print(var)


How can I change my code so that it doesn't have a dot before the word 'How' ?
That is, in general, I need to separate words like 'isHow' 'atWhen' 'goWhat' and put a dot between them, so that it would be like this 'is. How' 'at. When' 'go. What'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-06-03
@google_online

re.sub(r'(?<=[a-z])(?=[A-Z])', '. ', var)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question