[Solved] how to make a new line before a digit in a string [closed]


Use regex

a="HOW TO COOK RICE - Chapter 1: 1 rinse water once 2 add one and a half cup of water for every cup of rice 3 cover the pot and simmer"
idx=[i.start() for i in re.finditer('(\d{1,} \w{1,})', a)]
if idx:
    print(a[:idx[0]])
else:
    print(a)
for i,index in enumerate(idx):
    if not i==len(idx)-1:
        print(a[index:idx[i+1]])
    else:
        print(a[idx[i]:])

Output

HOW TO COOK RICE - Chapter 1: 
1 rinse water once 
2 add one and a half cup of water for every cup of rice 
3 cover the pot and simmer

8

solved how to make a new line before a digit in a string [closed]