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

前端 未结 12 980
眼角桃花
眼角桃花 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:16

    If the strings you are concatenating are literals, use String literal concatenation

    re.compile(
            "[A-Za-z_]"       # letter or underscore
            "[A-Za-z0-9_]*"   # letter, digit or underscore
        )
    

    This is useful if you want to comment on part of a string (as above) or if you want to use raw strings or triple quotes for part of a literal but not all.

    Since this happens at the syntax layer it uses zero concatenation operators.

提交回复
热议问题