Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question on Join() function
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question on None function in a machine learning algorithm Livingstone1337 1 632 Mar-17-2021, 10:12 PM
Last Post: supuflounder
  The difference between os.path.join( and os.sep.join( Pedroski55 2 1,448 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  Question in python function problem saratha 1 553 Jul-08-2020, 04:56 PM
Last Post: jefsummers
  question about python3 print function jamie_01 5 1,090 May-25-2020, 09:58 AM
Last Post: pyzyx3qwerty
  Question about the groupby function new_to_python 7 1,207 Feb-09-2020, 07:52 AM
Last Post: perfringo
  Question about list and while function doug2019 6 1,082 Oct-12-2019, 03:07 AM
Last Post: doug2019
  Threads join() question adam2020 4 1,331 Mar-19-2019, 09:30 PM
Last Post: adam2020
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 1,103 Feb-14-2019, 08:34 PM
Last Post: woooee
  question about list and remove function wmw12344 5 1,915 Mar-30-2018, 11:52 AM
Last Post: ljmetzger
  Basic input() function question rebubula76 2 1,788 Nov-27-2017, 08:12 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020