0

I have a string as follows:

”This 1 is just a 1 and 1 is just a number. And 1 + 1 is equal to 2”

When I want to use python format it would be something like:

var = 1
test = "This {} is just a {} and {} is just a number. And {} + {} is equal to 2".format(var,var,var,var,var)

But I would like to know if there is any quick replacement for str.format() in python for avoid repetition. Something like:

test = "This {} is just a {} and {} is just a number. And {} + {} is equal to 2".format(var*)
Braiano
  • 335
  • 6
  • 26
  • Does your format string "know" that all its placeholders are supposed to refer to the same value? Or do you just incidentally want to fill all placeholders with the same value? – deceze Sep 03 '19 at 07:50
  • The answers to this question, ['String formatting in Python 3'](https://stackoverflow.com/q/13945749/8944057), have some example string formatting usage that might be helpful. – eugenhu Sep 03 '19 at 07:53
  • You can do `print("This {v} is just a {v} and {v} is just a number. And {v} + {v} is equal to 2".format(v=var))`. Or with a f-string: `print(f"This {var} is just a {var} and {var} is just a number. And {var} + {var} is equal to 2")` – Niels Henkens Sep 03 '19 at 08:07
  • @deceze I think placeholders are supposed to refer to the same value, and probably a simple indexing should solve my problem. Thanks – Braiano Sep 03 '19 at 08:19

0 Answers0