How to multiply characters in string by number after each character in Python

1 Answer

0 votes
s = 'y5B2Q3p6'

new_s = ''

for ch in s:
    if ch.isdigit():
        new_s += new_s[-1] * (int(ch) - 1)
    else:
        new_s += ch

print(new_s)



'''
run:

yyyyyBBQQQpppppp

'''

 



answered Sep 25, 2019 by avibootz
...