Python Forum
Question on Join() function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Question on Join() function (/thread-18823.html)



Question on Join() function - sduvvuri - Jun-02-2019

Hi Friends,

I am completely new to coding & learning python from a book at home. So I have started from ABSOLUTE ZERO. Never done coding before in my life.

Right now I am at learning split() and join().

Question: When we precede a string value and .join(argument) - argument being a list, the 1st value in the list is not being joined with the string value. Can any knowledgeable people explain why?

For example:
phal = ['apple', 'banana', 'mango', 'orange']
desc = ' fruits. '.join(phal)
print(desc)
Result I get:
Output:
apple fruits.banana fruits.mango fruits.orange
So why isn't 'fruits.' not preceding 'apple' also and is only joining to the other 3 items in the list.

thank you very much for your time.


RE: Question on Join() function - buran - Jun-02-2019

that's by design - the str you specify is separator, i.e. join() takes a iterable of strings and turns them in a single str, by adding separator between elements


RE: Question on Join() function - perfringo - Jun-02-2019

There is built-in help always at your fingertips:

>>> help(str.join)
Help on method_descriptor:

join(self, iterable, /)
    Concatenate any number of strings.
    
    The string whose method is called is inserted in between each given string.
    The result is returned as a new string.
    
    Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
(END)