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 dir() function Soorya25 1 1,109 Jan-16-2023, 09:33 PM
Last Post: deanhystad
  Function not scriptable: Noob question kaega2 3 1,136 Aug-21-2022, 04:37 PM
Last Post: kaega2
  input function question barryjo 12 2,637 Jan-18-2022, 12:11 AM
Last Post: barryjo
  Question on None function in a machine learning algorithm Livingstone1337 1 2,332 Mar-17-2021, 10:12 PM
Last Post: supuflounder
  The difference between os.path.join( and os.sep.join( Pedroski55 2 9,310 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  Question in python function problem saratha 1 1,428 Jul-08-2020, 04:56 PM
Last Post: jefsummers
  question about python3 print function jamie_01 5 2,599 May-25-2020, 09:58 AM
Last Post: pyzyx3qwerty
  Question about the groupby function new_to_python 7 3,137 Feb-09-2020, 07:52 AM
Last Post: perfringo
  Question about list and while function doug2019 6 2,681 Oct-12-2019, 03:07 AM
Last Post: doug2019
  Threads join() question adam2020 4 2,569 Mar-19-2019, 09:30 PM
Last Post: adam2020

Forum Jump:

User Panel Messages

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