newdeveloper
newdeveloper

Reputation: 702

Dynamically format string using python format

I have to variables one="{}{}{}T{}:{}:{}" and two='2021,-10,-28,05,40,33' When i try to use print(one.format(two)) i am getting errors. IndexError: Replacement index 1 out of range for positional args tuple. guessing it is because the two variable is being seen as a string. So I switched and tried to do the *args method.

for item in two:
  item = str(item)
  data[item] = ''
result = template.format(*data)

However if I switch two='2021,-10,-28,00,00,00.478' it fails because a dictionary is unique. so how do i get the first method to work or is there a better solution.

Upvotes: 0

Views: 399

Answers (1)

Алексей Р
Алексей Р

Reputation: 7627

You should split the two into list and then unpack it with *

one="{}{}{}T{}:{}:{}"
two='2021,-10,-28,05,40,33'

print(one.format(*two.split(','))) # 2021-10-28T05:40:33

Upvotes: 3

Related Questions