There are always simple way and the other way. Code copy-paste is simple way and messing with pictures is the other. This just makes harder for other people to help.
Now to the problem: inserting "you know" after every 3 word.
Where should we start? Maybe from splitting text into words? As we don't need list as end result, we just generate stream of words:
So we stream enumerated words, if its third word we add 'you know' else just word.
Now to the problem: inserting "you know" after every 3 word.
Where should we start? Maybe from splitting text into words? As we don't need list as end result, we just generate stream of words:
s = "There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable. There is another theory mentioned, which states that this has already happened" print(*(word for word in s.split())) -->There is a theory /.../ has already happenedIt's looks like original string printed but it's actually streamed and printed word by word. One can ask - so what? If we are able to stream words, can we find right spot (after every third word) to insert 'you know'? Yes, we can. We just count words as we would do on paper. For such counting Python has built-in enumerate():
print(*enumerate(word for word in s.split())) --> (0, 'There') (1, 'is') (2, 'a') (3, 'theory') /.../ (45, 'has') (46, 'already') (47, 'happened')How do we determine third word? It's divisible by 3 with no reminder (
i % 3 == 0
). In order this to work we must start enumerating from 1 instead of default 0. So we stream enumerated words, if its third word we add 'you know' else just word.
insertion = 'you know' stream = enumerate((word for word in s.split()), start= 1) print(*(f'{word} {insertion}' if i % 3 == 0 else word for i, word in stream)) --> There is a you know theory which states you know that if ever you know anyone discovers exactly you know what the Universe you know is for and you know why it is you know here, it will you know instantly disappear and you know be replaced by you know something even more you know bizarre and inexplicable. you know There is another you know theory mentioned, which you know states that this you know has already happened you know
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.