Which is the preferred way to concatenate a string in Python?

前端 未结 12 855
眼角桃花
眼角桃花 2020-11-22 06:45

Since Python\'s string can\'t be changed, I was wondering how to concatenate a string more efficiently?

I can write like it:

s += string         


        
12条回答
  •  花落未央
    2020-11-22 07:21

    my use case was slight different. I had to construct a query where more then 20 fields were dynamic. I followed this approach of using format method

    query = "insert into {0}({1},{2},{3}) values({4}, {5}, {6})"
    query.format('users','name','age','dna','suzan',1010,'nda')
    

    this was comparatively simpler for me instead of using + or other ways

提交回复
热议问题