What exactly does the .join() method do?

后端 未结 9 2441
一向
一向 2020-11-22 10:54

I\'m pretty new to Python and am completely confused by .join() which I have read is the preferred method for concatenating strings.

I tried:

         


        
9条回答
  •  醉酒成梦
    2020-11-22 10:54

    To append a string, just concatenate it with the + sign.

    E.g.

    >>> a = "Hello, "
    >>> b = "world"
    >>> str = a + b
    >>> print str
    Hello, world
    

    join connects strings together with a separator. The separator is what you place right before the join. E.g.

    >>> "-".join([a,b])
    'Hello, -world'
    

    Join takes a list of strings as a parameter.

提交回复
热议问题