from itertools import product def subsl(text, **literals): return map( ''.join, product(*[map(str, literals.get(t, t)) for t in text]) ) print( *subsl('x^2+2*x*y+y^2', x=(1,2,3), y=(5,6,7)), sep='\n' )
Standard input is empty
1^2+2*1*5+5^2 1^2+2*1*5+6^2 1^2+2*1*5+7^2 1^2+2*1*6+5^2 1^2+2*1*6+6^2 1^2+2*1*6+7^2 1^2+2*1*7+5^2 1^2+2*1*7+6^2 1^2+2*1*7+7^2 1^2+2*2*5+5^2 1^2+2*2*5+6^2 1^2+2*2*5+7^2 1^2+2*2*6+5^2 1^2+2*2*6+6^2 1^2+2*2*6+7^2 1^2+2*2*7+5^2 1^2+2*2*7+6^2 1^2+2*2*7+7^2 1^2+2*3*5+5^2 1^2+2*3*5+6^2 1^2+2*3*5+7^2 1^2+2*3*6+5^2 1^2+2*3*6+6^2 1^2+2*3*6+7^2 1^2+2*3*7+5^2 1^2+2*3*7+6^2 1^2+2*3*7+7^2 2^2+2*1*5+5^2 2^2+2*1*5+6^2 2^2+2*1*5+7^2 2^2+2*1*6+5^2 2^2+2*1*6+6^2 2^2+2*1*6+7^2 2^2+2*1*7+5^2 2^2+2*1*7+6^2 2^2+2*1*7+7^2 2^2+2*2*5+5^2 2^2+2*2*5+6^2 2^2+2*2*5+7^2 2^2+2*2*6+5^2 2^2+2*2*6+6^2 2^2+2*2*6+7^2 2^2+2*2*7+5^2 2^2+2*2*7+6^2 2^2+2*2*7+7^2 2^2+2*3*5+5^2 2^2+2*3*5+6^2 2^2+2*3*5+7^2 2^2+2*3*6+5^2 2^2+2*3*6+6^2 2^2+2*3*6+7^2 2^2+2*3*7+5^2 2^2+2*3*7+6^2 2^2+2*3*7+7^2 3^2+2*1*5+5^2 3^2+2*1*5+6^2 3^2+2*1*5+7^2 3^2+2*1*6+5^2 3^2+2*1*6+6^2 3^2+2*1*6+7^2 3^2+2*1*7+5^2 3^2+2*1*7+6^2 3^2+2*1*7+7^2 3^2+2*2*5+5^2 3^2+2*2*5+6^2 3^2+2*2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