简体   繁体   中英

How to assign value from varibles to the enties of list

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 assit me how to implement this.

Use string formatting:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM