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?

1
  • Python overloads the * operator such that multiplying a String with an Int repeats the string. As such, you are after (var1 * n) + (var2 * n). Commented Sep 21, 2017 at 15:19

2 Answers 2

3

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

repeat = 3
output = 'evm --code ' + var1 * repeat + var2 * repeat + ' run'
Sign up to request clarification or add additional context in comments.

2 Comments

but the thing is- var1 should always be 1 less than var2, for some reason in this script they were equivalent
this works output = 'evm --code ' + var1 * repeat + var2 * (repeat - 1) + ' run'
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.

2 Comments

hmm, but the thing is var1 should always be 1 less than var2
so ? just use n-1 or some other logic to get the right amount of var1 and var2 repetitions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.