Mick M
Mick M

Reputation: 11

Repeating elements in string

Input is the number 25104, output is supposed to be 225555514444, therefore each number's count is the same as its value.

I've only found how to repeat the whole string but not elements within a string.

I don't even know where to start so can't include code.

Upvotes: 1

Views: 57

Answers (1)

Jab
Jab

Reputation: 27485

Using str.join and a comprehension:

>>> string = '25104'
>>> ''.join(s*int(s) for s in string)
225555514444

This will fail if if there is not an integer in the string because int will throw an error if unable to convert.

Upvotes: 2

Related Questions