Home > other >  How to assign value from varibles to the enties of list
How to assign value from varibles to the enties of list

Time:07-26

I have the following python code:

a = 2
b = 3
c = 1

command = ['entry1', 'entry2=', 'entry3=', 'entry4=']

I would like to assign the values of varibles a, b,c to entry2, entry3, entry4 respectively. So, command[1] needs to return entr2=2, command[2] entry3=3, command[3] entry4=1 respectively.

Could someone please assist me on how to implement this.

CodePudding user response:

Use string formatting:

command = ['entry1', f'entry2={a}', f'entry3={b}', f'entry4={c}']
  • Related