how can I add characters/numbers to blank spaces like this:
Today the ---- is cloudy, but there is no ----.
Today the --a)-- is cloudy, but there is no --b)--.(desired result)
As you can see blank spaces are not replaced with a fixed character, which makes using python replace()
method complicated for me.
You can use re.sub()
. It allows you to use a function as the replacement, so the function can increment the character each time it's called. I've written the function as a generator.
import re
def next_char():
char = 'a'
while True:
yield char
char = chr(ord(char) + 1)
if char > 'z':
char = 'a'
seq = next_char()
str = 'Today the ---- is cloudy, but there is no ----.'
str = re.sub(r'----', lambda x: ('--' + next(seq) + ')--'), str)
print(str)