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

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

    You write this function

    def str_join(*args):
        return ''.join(map(str, args))
    

    Then you can call simply wherever you want

    str_join('Pine')  # Returns : Pine
    str_join('Pine', 'apple')  # Returns : Pineapple
    str_join('Pine', 'apple', 3)  # Returns : Pineapple3
    

提交回复
热议问题