0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
smatthewenglish
  • 2,831
  • 4
  • 36
  • 72
  • Python overloads the `*` operator such that multiplying a `String` with an `Int` repeats the string. As such, you are after `(var1 * n) + (var2 * n)`. – Phylogenesis Sep 21 '17 at 15:19

2 Answers2

3

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

repeat = 3
output = 'evm --code ' + var1 * repeat + var2 * repeat + ' run'
ccbunney
  • 2,282
  • 4
  • 26
  • 42
1

In python, you can multiply a string by an int to repeat it a given number of times:

someString = "01"
someInt = 3
someString * someInt

will output:

'010101'

Knowing that, your problem should be trivial to solve. For example:

output = "evm --code %s%s run" % (var1 * n, var2 * n)

with n being a positive integer.

Note: above, I use a string format, which is better in many ways (and less error prone) than simple concatenation.

Derlin
  • 9,572
  • 2
  • 32
  • 53