简体   繁体   中英

Format string in python list

I have a list which should contain string with a particular format or character ie {{str(x), str(y)},{str(x), str(y)}} . I tried to do string concat like: "{{"+str(x), str(y)+"},{"+str(x), str(y)+"}}" and append to list, but it gets surrounded by brackets: [ ({{str(x), str(y)}),({str(x), str(y)}}) ] How can I get rid of the brackets or betterstill, is there a better approach to having a list without brackets like this: [{{string, string},{string, string}}]

The parentheses are because you're creating a tuple of three items:

  • "{{"+str(x)
  • str(y)+"},{"+str(x)
  • str(y)+"}}"

Try replacing those bare commas between str(x) and str(y) with +","+ :

"{{"+str(x)+","+str(y)+"},{"+str(x)+","str(y)+"}}"

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