Search code examples
pythonduplicatesvariable-length-array

Variable length string duplication in Python


The following short python script:

var1 = '7f0000000000000000000000000000000000000000000000000000000000000002'

var2 = '01'

output = 'evm --code ' + var1 + var1 + var2 + ' run'

print(output)

Is capable of generating the following string:

evm --code 7f00000000000000000000000000000000000000000000000000000000000000027f000000000000000000000000000000000000000000000000000000000000000201 run

However, I'd like to generate strings wherein var1 can be appended to the leftmost side of the output string for a pre-specified (parameterized) number of times. Corresponding to each time we append var1 to the leftmost side, I'd like to append var2 to the rightmost side the same number of times.

So with the above output string as a baseline, if we select 3 as our parameter, our new output string should render as follows:

evm --debug --code 7f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000027f000000000000000000000000000000000000000000000000000000000000000201010101 run 

How could I control the duplication of those strings, appending them to that base string as described above, with a variable?


Solution

  • You can use the multiplier operator on a string, e.g.:

    repeat = 3
    output = 'evm --code ' + var1 * repeat + var2 * repeat + ' run'